Problem/Motivation

Follow-up from #2690007: Remove drupal_render deprecated method and refactor non-functional cleanups (see comment #29).

In that issue we have converted some entity manager wrapper in EntityReferenceRevisionsEntityFormatter (to call getViewModeOptions) and EntityReferenceRevisionsItem (to call loadEntityByUuid), and we need to test entity reference revisions default values and configuration dependencies.

Proposed resolution

  1. Basically, what you need to do is the following:

    'default_value' => [
    [
    'target_id' => $composite->id(),
    'revision_id' => $composite->getRevisionId(),
    ]
    ]

    To the field config, so it uses that reference as the default. When you try that, you'll notice that you get an invalid config schema error, we are missing schema for the field type value.

  2. add missing schema for field type value

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#35 test_err_default_values_and_config_dependencies-2711429-35.patch11.79 KBtduong
#35 test_err_default_values_and_config_dependencies-2711429-35-test_only.patch8.76 KBtduong
#35 interdiff-2711429-32-35.txt1.4 KBtduong
#32 test_err_default_values_and_config_dependencies-2711429-32.patch11.54 KBtduong
#32 test_err_default_values_and_config_dependencies-2711429-32-test_only.patch8.51 KBtduong
#32 interdiff-2711429-29-32.txt8.94 KBtduong
#29 test_err_default_values_and_config_dependencies-2711429-29.patch6.14 KBtduong
#29 interdiff-2711429-26-29.txt2.31 KBtduong
#26 test_err_default_values_and_config_dependencies-2711429-26.patch5.17 KBtduong
#26 test_err_default_values_and_config_dependencies-2711429-26-test_only.patch3.56 KBtduong
#26 interdiff-2711429-23-26.txt1.57 KBtduong
#23 test_err_default_values_and_config_dependencies-2711429-23.patch5.04 KBtduong
#23 test_err_default_values_and_config_dependencies-2711429-23-test_only.patch3.5 KBtduong
#23 interdiff-2711429-20-23.txt3.12 KBtduong
#20 test_err_default_values_and_config_dependencies-2711429-20.patch5.47 KBtduong
#20 test_err_default_values_and_config_dependencies-2711429-20-test_only.patch3.51 KBtduong
#20 interdiff-2711429-17-20.txt8.17 KBtduong
#17 test_err_default_values_and_config_dependencies-2711429-17-interdiff.txt1.83 KBberdir
#17 test_err_default_values_and_config_dependencies-2711429-17.patch11.2 KBberdir
#14 test_err_default_values_and_config_dependencies-2711429-14.patch10.71 KBtduong
#14 test_err_default_values_and_config_dependencies-2711429-14-test_only.patch9.23 KBtduong
#14 interdiff-2711429-11-14.txt9.27 KBtduong
#11 test_err_default_values_and_config_dependencies-2711429-11.patch4.51 KBtduong
#11 test_err_default_values_and_config_dependencies-2711429-11-test_only.patch3.04 KBtduong
#11 interdiff-2711429-7-11.txt6.93 KBtduong
#7 test_err_default_values_and_config_dependencies-2711429-7.patch5.87 KBtduong
#7 test_err_default_values_and_config_dependencies-2711429-7-test_only.patch4.4 KBtduong
#5 interdiff-2711429-2-5.txt2.72 KBtduong
#5 test_err_default_values_and_config_dependencies-2711429-5.patch5.87 KBtduong
#5 test_err_default_values_and_config_dependencies-2711429-5-test_only.patch4.4 KBtduong
#2 test_err_default_values_and_config_dependencies-2711429-2.patch4.03 KBtduong

Comments

tduong created an issue. See original summary.

tduong’s picture

Status: Active » Needs review
StatusFileSize
new4.03 KB

Here there is the test that get the missing config schema error message.

Status: Needs review » Needs work
tduong’s picture

Issue summary: View changes
tduong’s picture

Assigned: Unassigned » tduong
Status: Needs work » Needs review
StatusFileSize
new4.4 KB
new5.87 KB
new2.72 KB

Added schema for field type default value.

miro_dietiker’s picture

Please don't mix unrelated code cleanups with functional fixes and adding tests.
It's really hard to review code changes to then realise they have no impact.. and finally finding the relevant pieces in all the changes.

The functional changes look pretty fine.

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Tests/EntityReferenceRevisionsCompositeTest.php
@@ -44,38 +44,47 @@ class EntityReferenceRevisionsCompositeTest extends WebTestBase {
    */
   public function testEntityReferenceRevisionsCompositeRelationship() {
 
+    // Create the test composite entity.
+    $composite = EntityTestCompositeRelationship::create([
+      'uuid' => '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410',
+      'name' => $this->randomMachineName(),
+    ]);
+    $composite->save();
+
     // Create the reference to the composite entity test.
-    $field_storage = FieldStorageConfig::create(array(

This is not related to composite. In fact, having a default value with composite doesn't really work.

Lets make a new test instead.

tduong’s picture

Moved test to EntityReferenceRevisionsAdminTest and fixed it, used a Node instead of a composite entity. Reverted everything from EntityReferenceRevisionsCompositeTest.

berdir’s picture

Status: Needs review » Needs work

Yes, having the test separate is much better to review.

Not sure if we shouldn't put it in a separate test class, possibly also a kernel test since we don't need a UI.

  1. +++ b/src/Tests/EntityReferenceRevisionsAdminTest.php
    @@ -109,4 +112,62 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
    +    // Create a node with a reference to the test target node.
    +    $node2 = $this->createNode([
    +      'title' => $this->randomMachineName(),
    +      'type' => 'article',
    +      'target_node_reference' => $node1,
    +    ]);
    

    To test that the default value works, you want to create the node without explicitly specifying the node.

  2. +++ b/src/Tests/EntityReferenceRevisionsAdminTest.php
    @@ -109,4 +112,62 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
    +
    +    // Test for loadEntityByUuid() in EntityReferenceRevisionsItem::calculateDependencies().
    +    $node1_after = Node::load($node1->id())->toArray();
    +    $this->assertEqual($node1_after['uuid'][0]['value'], '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410');
    +
    

    This is not checking anything, just your own code above. It doesn't call that function.

    Instead, you simply want to check that $node2->target_node_reference->entity has the same id and revision id as $node 1.

    Additionally, after setting the default value, save $node1 with a new revision_id, keep the old one. The default value should reference the first version, not the new one.

    Second, instead of $node1 and $node2, use $node_target and $node_host or something like that.

  3. +++ b/src/Tests/EntityReferenceRevisionsAdminTest.php
    @@ -109,4 +112,62 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
    +    // Check if the ERR default values are properly created.
    +    $default_reference = $field->getDefaultValue($node2);
    +    $this->assertEqual($default_reference[0]['target_id'], $node1->id());
    +    $this->assertEqual($default_reference[0]['revision_id'], $node1->getRevisionId());
    

    Then this isn't needed.

  4. +++ b/src/Tests/EntityReferenceRevisionsAdminTest.php
    @@ -109,4 +112,62 @@ class EntityReferenceRevisionsAdminTest extends WebTestBase {
    +    // Check if the configuration dependencies are properly created.
    +    $dependencies = $field->calculateDependencies()->getDependencies();
    +    $this->assertEqual($dependencies['config'][0], 'field.storage.node.target_node_reference');
    +    $this->assertEqual($dependencies['config'][1], 'node.type.article');
    +    $this->assertEqual($dependencies['module'][0], 'entity_reference_revisions');
    

    This is the part that actually checks the default value dependencies.

    This is where you need to check for the UUID of the content entity, if it's missing, then it doesn't work yet.

tduong’s picture

Autocomplete is not working, unless I explicitly set it on node creation. Tried to set entity form/view display to check it before/after saving the new node, but the default value is actually set correctly... I don't have any more ideas what is happening and how to fix it...

Copied also in a unit test, I have some random problem with setComponent().

Status: Needs review » Needs work
berdir’s picture

Ok, took me quite a bit of debugging too but the problem was actually trivial. It's target_revision_id, not just revision_id.

As written above, I'd prefer to have this test method in \Drupal\Tests\entity_reference_revisions\Kernel\EntityReferenceRevisionsSaveTest, remove all the UI and login stuff.

berdir’s picture

Status: Needs review » Needs work

tduong’s picture

Aaah these trivial bugs...
Removed test from the web test and updated the unit test one.
Thanks for the big feedback :)

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Field/FieldType/EntityReferenceRevisionsItem.php
    @@ -23,6 +23,8 @@ use Drupal\entity_reference_revisions\EntityNeedsSaveInterface;
      *   allowed bundles.
    + * - target_id: The entity ID to reference to, used as default value.
    + * - revision_id: The revision ID.
      *
      * @FieldType(
    

    Those are not settings, they are default value (and also wrong, now).

    => remove.

  2. +++ b/tests/src/Kernel/EntityReferenceRevisionsSaveTest.php
    @@ -184,4 +184,75 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
    +    $field->save();
    +
    +    // Add reference values to field config that will be used as default value.
    +    $default_value = [
    +      [
    

    you shouldn't have to save the field twice, remove the first call.

  3. +++ b/tests/src/Kernel/EntityReferenceRevisionsSaveTest.php
    @@ -184,4 +184,75 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
    +    // Set new revision_id to the test target node.
    

    We don't really set: // Resave the target node so that the default revision is not he one we want to use.

  4. +++ b/tests/src/Kernel/EntityReferenceRevisionsSaveTest.php
    @@ -184,4 +184,75 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
    +    // Check if the ERR default values are properly created.
    +    $this->assertTrue($node_target_after->getRevisionId() != $revision_id);
    

    If you do want to assert this then move it up and do it after resaving the node.

  5. +++ b/tests/src/Kernel/EntityReferenceRevisionsSaveTest.php
    @@ -184,4 +184,75 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
    +    $node_target_after = $node_target_after->toArray();
    +    $node_host_after = Node::load($node_host->id())->toArray();
    +    $this->assertEquals($node_target_after['uuid'][0]['value'], '2d04c2b4-9c3d-4fa6-869e-ecb6fa5c9410');
    +    $this->assertEquals($node_host_after['target_node_reference'][0]['target_id'], $node_target->id());
    +    $this->assertEquals($node_host_after['target_node_reference'][0]['target_revision_id'], $revision_id);
    

    It's way easier to access those values on the node object instead of using toArray(). Just write $node_host_after->target_node_reference->target_id.

    The uuid check is also unnecessary and unrelated.

  6. +++ b/tests/src/Kernel/EntityReferenceRevisionsSaveTest.php
    @@ -184,4 +184,75 @@ class EntityReferenceRevisionsSaveTest extends KernelTestBase {
    +    // Check if the configuration dependencies are properly created.
    +    $dependencies = $field->calculateDependencies()->getDependencies();
    +    $this->assertEquals($dependencies['config'][0], 'field.storage.node.target_node_reference');
    +    $this->assertEquals($dependencies['config'][1], 'node.type.article');
    +    $this->assertEquals($dependencies['module'][0], 'entity_reference_revisions');
    

    This missing the actually relevant part here. The content dependency on the uuid of $node_target.

tduong’s picture

1-5 done.
6. not sure how to get the uuid from content dependency (?)

berdir’s picture

Status: Needs review » Needs work

It has to be in $dependencies. If it is not there, then it's not working yet and you need to debug calculateDependencies().

tduong’s picture

Added target_uuid to the default value configuration/schema, fixed the test.

berdir’s picture

This means we're not testing that the target_uuid is actually stored properly when configuring this in the UI. Not sure if that's worth doing.

I'm also not sure if we need our custom code for this. Is the calculateDependencies() method different from the parent class? Maybe we can just remove it.

Without a revision UUID, it's pretty bogus anyway.

tduong’s picture

This means we're not testing that the target_uuid is actually stored properly when configuring this in the UI.

Debugging $default_value in EntityReferenceRevisionsItem::calculateDependencies() there are only our target_id and target_revision_id, so I thought that we were missing target_uuid in the schema, but yeah, wasn't sure that it was the right thing or just a "hardcoded workaround"...

I'm also not sure if we need our custom code for this. Is the calculateDependencies() method different from the parent class? Maybe we can just remove it.

Not sure, I've put a debug in the parent method, but it doesn't even print anything from there... and in EntityReferenceRevisionsItem::calculateDependencies() we never call the parent method...

Status: Needs review » Needs work
berdir’s picture

I think you misunderstood me.

target_uuid isn't going to magically work. We do need it.

The only question I have is if it is actually set if you select a default in the UI. I think we use standard widgets and extend from core, so it *should* work. Not sure if it's worth to add a test for this. Looking at \Drupal\entity_reference_revisions\Tests\EntityReferenceRevisionsAdminTest::testEntityReferenceRevisions, it might just work if you create the node before the field and then put that node into the $field_edit argument of static::fieldUIAddNewField(). Then you can check the UI part there and also make sure that adding it actually adds the target_uuid to the field default value.

What I mean with calculateDependencies() is that as far as I see, it's pretty much a duplicate of the parent implementation. I would recommend you just remove, and if the test still passes, then I think we can just move forward with the core implementation.

tduong’s picture

Yes, thank you for the hints!
Removed EntityReferenceRevisionsItem::calculateDependencies(), fixed EntityReferenceRevisionsSaveTest::testEntityReferenceRevisionsDefaultValue() to cover the dependencies properties part and extended EntityReferenceRevisionsAdminTest:: testEntityReferenceRevisions() to test the default value target_uuid part.

berdir’s picture

Status: Needs review » Needs work

Small misunderstanding, I didn't mean to remove the uuid from the kernel test, we still want to have that there. Just re-add the removed/commented code from before.

tduong’s picture

Reverted changes in the kernel test.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Now we're good to go I think.

miro_dietiker’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

Status: Fixed » Closed (fixed)

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