NodeFormSaveChangedTimeTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Tests updating the changed time after API and FORM entity save.
  6. *
  7. * @group node
  8. */
  9. class NodeFormSaveChangedTimeTest extends BrowserTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = [
  16. 'node',
  17. ];
  18. /**
  19. * An user with permissions to create and edit articles.
  20. *
  21. * @var \Drupal\user\UserInterface
  22. */
  23. protected $authorUser;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. // Create a node type.
  30. $this->drupalCreateContentType([
  31. 'type' => 'article',
  32. 'name' => 'Article',
  33. ]);
  34. $this->authorUser = $this->drupalCreateUser(['access content', 'create article content', 'edit any article content'], 'author');
  35. $this->drupalLogin($this->authorUser);
  36. // Create one node of the above node type .
  37. $this->drupalCreateNode([
  38. 'type' => 'article',
  39. ]);
  40. }
  41. /**
  42. * Test the changed time after API and FORM save without changes.
  43. */
  44. public function testChangedTimeAfterSaveWithoutChanges() {
  45. $storage = $this->container->get('entity_type.manager')->getStorage('node');
  46. $storage->resetCache([1]);
  47. $node = $storage->load(1);
  48. $changed_timestamp = $node->getChangedTime();
  49. $node->save();
  50. $storage->resetCache([1]);
  51. $node = $storage->load(1);
  52. $this->assertEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time wasn't updated after API save without changes.");
  53. // Ensure different save timestamps.
  54. sleep(1);
  55. // Save the node on the regular node edit form.
  56. $this->drupalPostForm('node/1/edit', [], t('Save'));
  57. $storage->resetCache([1]);
  58. $node = $storage->load(1);
  59. $this->assertNotEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time was updated after form save without changes.");
  60. }
  61. }