0

I am trying to return the common parent of 2 entities from their revision graph. For ex: 1->2->3->4->5 is a linear graph with 1 as original node and rest all are revisions. So, I wrote a function to take 2 revision ID as argument and return their parent (3 is parent of 4 and 5). The code is

<?php

namespace Drupal\conflict;

use Drupal\conflict\ConflictManagerInterface;
use Drupal\conflict\ConflictAncestorResolverInterface;

class ConflictManager implements ConflictManagerInterface {

    protected $resolvers = [];

    public function applies() {
        return TRUE;
    }

    public function addAncestorResolver(ConflictAncestorResolverInterface $resolver)
    {
         $this->resolvers[] = $resolver;
    }

    public function resolveLowestCommonAncestor($revision1, $revision2)
    {
        foreach ($this->resolvers as $resolver) {
            if ($resolver->applies()) {
                return $resolver->resolve($revision1, $revision2);
            }  
        }
        echo "No revision found";
        return -1;
    }

    public function resolve($revision1, $revision2) {
        if ($revision1 < $revision2) {
            $par_revision = $revision1-1;
            return $par_revision;
        }
        $par_revision = $revision2-1;
        return $par_revision;
    }
 }

For this code, I have written a Kernel test:

<?php

namespace Drupal\Tests\conflict\Kernel;

use Drupal;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\conflict;

/**
* @group conflict
*/
class KernelLcaTest extends KernelTestBase {
   /**
    * Modules to enable.
    *
    * @var array
    */
   public static $modules = ['entity_test'];

   public function testsimple  () {
       $entity = EntityTest::create(['label' => 'revision 1']);
       $entity->save();
       $entity->label = ['revision 2'];
       $entity->save();
       $entity->label = ['revision 3'];
       $entity->save();

       $revision2 = entity_revision_load('entity_test', 2);
       $revision3 = entity_revision_load('entity_test', 3);

       $manager = Drupal::service('conflict.conflict_manager');
       $revisionLca = $manager->resolveLowestCommonAncestor($revision2, $revision3);
       $this->assertTrue($revisionLca->label() == 'revision 1');
    } 
}

When i run this test through CLI with run-tests.sh, I get the following errors:

Drupal\Tests\conflict\Kernel\KernelLcaTest::testsimple Drupal\Component\Plugin\Exception\PluginNotFoundException: The "user" entity type does not exist. /var/www/html/drupal8/core/lib/Drupal/Core/Entity/EntityTypeManager.php:125 /var/www/html/drupal8/core/lib/Drupal/Core/Entity/EntityManager.php:46 /var/www/html/drupal8/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php:66 /var/www/html/drupal8/core/lib/Drupal/Core/Field/BaseFieldDefinition.php:539 /var/www/html/drupal8/core/lib/Drupal/Core/Field/TypedData/FieldItemDataDefinition.php:66 /var/www/html/drupal8/core/lib/Drupal/Core/Field/FieldItemBase.php:50 /var/www/html/drupal8/core/lib/Drupal/Core/TypedData/TypedData.php:46 /var/www/html/drupal8/core/lib/Drupal/Core/TypedData/TypedDataManager.php:91 /var/www/html/drupal8/core/lib/Drupal/Core/TypedData/TypedDataManager.php:104 /var/www/html/drupal8/core/lib/Drupal/Core/TypedData/TypedDataManager.php:187 /var/www/html/drupal8/core/lib/Drupal/Core/Field/FieldTypePluginManager.php:82 /var/www/html/drupal8/core/lib/Drupal/Core/Field/FieldItemList.php:40 /var/www/html/drupal8/core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php:66 /var/www/html/drupal8/core/lib/Drupal/Core/Field/FieldItemList.php:118 /var/www/html/drupal8/core/lib/Drupal/Core/Field/FieldItemList.php:181 /var/www/html/drupal8/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php:112 /var/www/html/drupal8/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php:88 /var/www/html/drupal8/core/lib/Drupal/Core/Entity/EntityStorageBase.php:184 /var/www/html/drupal8/core/lib/Drupal/Core/Entity/Entity.php:509 /var/www/html/drupal8/modules/conflict/tests/src/Kernel/KernelLcaTest.php:22

Any kind of help would be appreciated. Thanks.

3

https://drupal.stackexchange.com/a/205919/42650 is related, specifically this part: "make sure the necessary modules are enabled".

In kernel tests, no modules are enabled by default. Anything, even required modules like user and system have to be enabled. That's one benefit of extending from EntityKernelTestBase as suggested there, that installs a bunch of things so you can have test entities with user references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.