Support from Acquia helps fund testing for Drupal Acquia logo

Comments

valthebald created an issue. See original summary.

marvin_B8’s picture

Status: Active » Needs review
FileSize
3.45 KB
Mile23’s picture

Status: Needs review » Needs work

The patch applies and it handles all useages of entity_load*'node', but there's one thing:

+++ b/core/modules/node/src/Tests/NodeFormSaveChangedTimeTest.php
@@ -52,11 +53,14 @@ protected function setUp() {
   public function testChangedTimeAfterSaveWithoutChanges() {
-    $node = entity_load('node', 1);
+    $node = Node::load(1);
     $changed_timestamp = $node->getChangedTime();
 
     $node->save();
-    $node = entity_load('node', 1, TRUE);
+    $storage = $this->container->get('entity_type.manager')->getStorage('node');
+    $storage->resetCache([1]);
+
+    $node = $storage->load(1);
     $this->assertEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time wasn't updated after API save without changes.");
 
     // Ensure different save timestamps.
@@ -65,7 +69,10 @@ public function testChangedTimeAfterSaveWithoutChanges() {

@@ -65,7 +69,10 @@ public function testChangedTimeAfterSaveWithoutChanges() {
     // Save the node on the regular node edit form.
     $this->drupalPostForm('node/1/edit', array(), t('Save'));
 
-    $node = entity_load('node', 1, TRUE);
+    $storage = $this->container->get('entity_type.manager')->getStorage('node');
+    $storage->resetCache([1]);
+
+    $node = $storage->load(1);
     $this->assertNotEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time was updated after form save without changes.");
   }

Get $storage once at the top of the method, and then use it in all the different places, including replacing Node::load().

gaurav.pahuja’s picture

Status: Needs work » Needs review
FileSize
3.36 KB
640 bytes

Done.
Please check.

valthebald’s picture

Status: Needs review » Needs work

Good progress! I have some small notes though:
1.

    $storage = $this->container->get('entity_type.manager')->getStorage('node');
    // Load nodes with only a condition. Nodes 3 and 4 will be loaded.
    $nodes = $storage->loadByProperties(array('promote' => 0));

$storage variable is used only once, can be just

    // Load nodes with only a condition. Nodes 3 and 4 will be loaded.
    $nodes = $this->container->get('entity_type.manager')->getStorage('node')
      ->loadByProperties(array('promote' => 0));

2. In NodeFormSaveChangedTimeTest.php

I think added empty lines reduce readability, not increase it

Sonal.Sangale’s picture

Assigned: Unassigned » Sonal.Sangale
Sonal.Sangale’s picture

Status: Needs work » Needs review
FileSize
3.34 KB
2.27 KB

Added suggested changes.

Mile23’s picture

Status: Needs review » Needs work
+++ b/core/modules/node/src/Tests/NodeFormSaveChangedTimeTest.php
@@ -52,11 +53,12 @@ protected function setUp() {
   public function testChangedTimeAfterSaveWithoutChanges() {
-    $node = entity_load('node', 1);
+    $node = Node::load(1);
     $changed_timestamp = $node->getChangedTime();
-
     $node->save();
-    $node = entity_load('node', 1, TRUE);
+    $storage = $this->container->get('entity_type.manager')->getStorage('node');
+    $storage->resetCache([1]);
+    $node = $storage->load(1);
     $this->assertEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time wasn't updated after API save without changes.");
 
     // Ensure different save timestamps.

Right, that's what I was referring to in #3. Node::load(1); can be replaced with $storage->load(1).

So: Replace Node::load(1) with a call to $storage->load(1). In order to do that, get $storage at the top of the function instead of the middle.

The reason to do this is: Node::load() gets the storage service from \Drupal, which is generally bad for tests. In other circumstances it doesn't matter so much, and if we weren't already getting the storage service I wouldn't bring it up.

gaurav.pahuja’s picture

Status: Needs work » Needs review
FileSize
3.35 KB
989 bytes
ashishdalvi’s picture

FileSize
3.32 KB
990 bytes

Hi Mile23,

Worked on suggestion mentioned. Please review.

ashishdalvi’s picture

Assigned: Sonal.Sangale » Unassigned

Sorry, Didn't noticed previously added patch.

valthebald’s picture

Status: Needs review » Needs work

There is no such thing as Node::loadByProperties() - tests pass only because this call is in comment.
This should be \Drupal::entityTypeManager()->getStorage('node')->loadByProperties()

ashishdalvi’s picture

Assigned: Unassigned » ashishdalvi
ashishdalvi’s picture

Worked on suggested changes.

ashishdalvi’s picture

Assigned: ashishdalvi » Unassigned
Status: Needs work » Needs review
valthebald’s picture

Status: Needs review » Reviewed & tested by the community

Patch #14 contains all recommendations that were given, and there are no more occurences of entity_load*'node'

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x, thanks!

xjm’s picture

Status: Fixed » Reviewed & tested by the community

So something went strange with this and it is not in HEAD. I think it (or part of it) got committed along with #2723589: Remove entity_load* usage for filter_format entity type, and then HEAD broke.

Setting this issue back to RTBC and sending for a retest.

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Alright, I took a look at the actual patch here now that we know it doesn't break HEAD when it's applied as a whole. ;)

+++ b/core/modules/node/src/Tests/NodeFormSaveChangedTimeTest.php
@@ -52,11 +53,11 @@ protected function setUp() {
   public function testChangedTimeAfterSaveWithoutChanges() {
-    $node = entity_load('node', 1);
+    $storage = $this->container->get('entity_type.manager')->getStorage('node');
+    $storage->resetCache([1]);
+    $node = $storage->load(1);
     $changed_timestamp = $node->getChangedTime();
-
     $node->save();
-    $node = entity_load('node', 1, TRUE);
     $this->assertEqual($changed_timestamp, $node->getChangedTime(), "The entity's changed time wasn't updated after API save without changes.");

This appears to be changing the logic of the test. It's no longer ensuring the entity is reloaded with the cache reset after the save() call, which seems to me to be the point of the test.

Sagar Ramgade’s picture

Patch attached addresses #19 by @xjm.

Sagar Ramgade’s picture

Status: Needs work » Needs review
naveenvalecha’s picture

+++ b/core/modules/node/src/Tests/NodeFormSaveChangedTimeTest.php
@@ -2,6 +2,7 @@
+use Drupal\node\Entity\Node;

are we using this Node class ? if not remove it.

valthebald’s picture

Status: Needs review » Needs work

Second to what @naveenvalecha said

Sagar Ramgade’s picture

Status: Needs work » Needs review
FileSize
3.31 KB
420 bytes

Thank you Naveen for catching that, interdiff and patch attached.

valthebald’s picture

Status: Needs review » Reviewed & tested by the community

Switching to RTBC again

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed fc189a4 and pushed to 8.2.x. Thanks!

  • alexpott committed fc189a4 on 8.2.x
    Issue #2723611 by Ashish.Dalvi, Sagar Ramgade, gaurav.pahuja, Sonal....

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.