You are here

class ContentHubEntityDependencyTest in Acquia Content Hub 8

PHPUnit for the ContentHubEntityDependency class.

@coversDefaultClass \Drupal\acquia_contenthub\ContentHubEntityDependency

@group acquia_contenthub

Hierarchy

Expanded class hierarchy of ContentHubEntityDependencyTest

File

tests/src/Unit/ContentHubEntityDependencyTest.php, line 16

Namespace

Drupal\Tests\acquia_contenthub\Unit
View source
class ContentHubEntityDependencyTest extends UnitTestCase {
  use ContentHubEntityTrait;

  /**
   * Returns the module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * A Content Hub Entity.
   *
   * @var \Acquia\ContentHubClient\Entity|\PHPUnit_Framework_MockObject_MockObject
   */
  private $entity;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Setting up the Module Handler.
    $this->moduleHandler = $this
      ->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');

    // Setting up the Container.
    $this->container = $this
      ->createMock('Drupal\\Core\\DependencyInjection\\Container');
    $this->container
      ->method('get')
      ->willReturnCallback(function ($name) {
      switch ($name) {
        case 'module_handler':
          return $this->moduleHandler;
      }
      return NULL;
    });
    \Drupal::setContainer($this->container);
  }

  /**
   * Test for getUuid() and getEntityType() method.
   *
   * @covers ::getUuid
   * @covers ::getEntityType
   */
  public function testGetUuidAndType() {
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $this
      ->assertEquals($this->entity
      ->getUuid(), $ch_entity_dependency
      ->getUuid());
    $this
      ->assertEquals('node', $ch_entity_dependency
      ->getEntityType());
  }

  /**
   * Test for isEntityDependent() method.
   *
   * @covers ::isEntityDependent
   */
  public function testIsEntityDependent() {

    // A 'node' entity is an 'independent' entity.
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $this
      ->assertFalse($ch_entity_dependency
      ->isEntityDependent());

    // A 'paragraph' entity is a 'dependent' entity.
    $values = [
      'type' => 'paragraph',
    ];
    $this->entity = $this
      ->createContentHubEntity($values);
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $this
      ->assertTrue($ch_entity_dependency
      ->isEntityDependent());
  }

  /**
   * Test for getRelationship() method.
   *
   * @covers ::getRelationship
   */
  public function testGetRelationship() {
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $this
      ->assertEquals(ContentHubEntityDependency::RELATIONSHIP_INDEPENDENT, $ch_entity_dependency
      ->getRelationship());
  }

  /**
   * Test Dependency Chain.
   *
   * @covers ::appendDependencyChain
   * @covers ::isInDependencyChain
   * @covers ::getDependencyChain
   */
  public function testDependencyChain() {
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);

    // Adding dependency.
    $entity1 = $this
      ->createContentHubEntity([
      'uuid' => '00000000-2222-0000-0000-000000000000',
    ]);
    $ch_entity_dependency1 = new ContentHubEntityDependency($entity1);
    $ch_entity_dependency
      ->appendDependencyChain($ch_entity_dependency1);
    $expected_dependency_chain = [];
    $expected_dependency_chain[] = $entity1
      ->getUuid();
    $this
      ->assertEquals($expected_dependency_chain, $ch_entity_dependency
      ->getDependencyChain());

    // Adding another dependency.
    $entity2 = $this
      ->createContentHubEntity([
      'uuid' => '00000000-3333-0000-0000-000000000000',
    ]);
    $ch_entity_dependency2 = new ContentHubEntityDependency($entity2);
    $ch_entity_dependency
      ->appendDependencyChain($ch_entity_dependency2);
    $expected_dependency_chain[] = $entity2
      ->getUuid();
    $this
      ->assertEquals($expected_dependency_chain, $ch_entity_dependency
      ->getDependencyChain());

    // $entity1 and $entity2 should be in the dependency chain.
    $this
      ->assertTrue($ch_entity_dependency
      ->isInDependencyChain($ch_entity_dependency1));
    $this
      ->assertTrue($ch_entity_dependency
      ->isInDependencyChain($ch_entity_dependency2));

    // This entity should not be in the dependency chain.
    $entity3 = $this
      ->createContentHubEntity([
      'uuid' => '00000000-4444-0000-0000-000000000000',
    ]);
    $ch_entity_dependency3 = new ContentHubEntityDependency($entity3);
    $this
      ->assertFalse($ch_entity_dependency
      ->isInDependencyChain($ch_entity_dependency3));
  }

  /**
   * Test Dependency Chain.
   *
   * @covers ::setParent
   * @covers ::getParent
   */
  public function testParenthood() {
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);

    // Adding a parent.
    $entity1 = $this
      ->createContentHubEntity([
      'uuid' => '00000000-2222-0000-0000-000000000000',
    ]);
    $ch_entity_dependency1 = new ContentHubEntityDependency($entity1);
    $ch_entity_dependency
      ->setParent($ch_entity_dependency1);
    $this
      ->assertEquals($ch_entity_dependency1, $ch_entity_dependency
      ->getParent());
  }

  /**
   * Test getRawEntity method.
   *
   * @covers ::getRawEntity
   */
  public function testGetRawEntity() {
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $this
      ->assertEquals($this->entity, $ch_entity_dependency
      ->getRawEntity());
  }

  /**
   * Test setAuthor method.
   *
   * @covers ::setAuthor
   */
  public function testSetAuthor() {
    $author = '00000000-9999-0000-0000-000000000000';
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $ch_entity_dependency
      ->setAuthor($author);
    $cdf = $ch_entity_dependency
      ->getRawEntity();
    $expected = [
      'type' => Attribute::TYPE_REFERENCE,
      'value' => [
        'en' => $author,
      ],
    ];
    $this
      ->assertEquals($expected, $cdf['attributes']['author']);
  }

  /**
   * Test setStatus method.
   *
   * @covers ::setStatus
   */
  public function testSetStatus() {
    $status = 1;
    $this->entity = $this
      ->createContentHubEntity();
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $ch_entity_dependency
      ->setStatus($status);
    $cdf = $ch_entity_dependency
      ->getRawEntity();
    $expected = [
      'type' => Attribute::TYPE_INTEGER,
      'value' => [
        'en' => $status,
      ],
    ];
    $this
      ->assertEquals($expected, $cdf['attributes']['status']);
  }

  /**
   * Test getRemoteDependencies method.
   *
   * @covers ::getRemoteDependencies
   */
  public function testGetRemoteDependencies() {
    $this->moduleHandler
      ->expects($this
      ->at(0))
      ->method('moduleExists')
      ->with('entity_embed')
      ->willReturn(TRUE);
    $values['attributes']['field_reference1'] = [
      'type' => Attribute::TYPE_REFERENCE,
      'value' => [
        'en' => '00000000-2222-0000-0000-000000000000',
      ],
    ];
    $values['attributes']['field_reference2'] = [
      'type' => Attribute::TYPE_ARRAY_REFERENCE,
      'value' => [
        'en' => [
          '00000000-3333-0000-0000-000000000000',
          '00000000-4444-0000-0000-000000000000',
          '00000000-5555-0000-0000-000000000000',
        ],
      ],
    ];

    // Author should be excluded from dependency calculation.
    $values['attributes']['author'] = [
      'type' => Attribute::TYPE_REFERENCE,
      'value' => [
        'en' => '00000000-6666-0000-0000-000000000000',
      ],
    ];

    // Embedded entities should be included in the dependencies.
    $body_value = [
      'value' => '<drupal-entity data-caption="some_image" data-embed-button="media_browser" data-entity-embed-display="view_mode:media.embedded" data-entity-type="media" data-entity-uuid="00000000-7777-0000-0000-000000000000"></drupal-entity>',
      'summary' => '',
      'format' => 'rich_text',
    ];
    $values['attributes']['body'] = [
      'type' => Attribute::TYPE_ARRAY_STRING,
      'value' => [
        'en' => [
          json_encode($body_value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
        ],
      ],
    ];
    $this->entity = $this
      ->createContentHubEntity($values);
    $ch_entity_dependency = new ContentHubEntityDependency($this->entity);
    $dependencies = $ch_entity_dependency
      ->getRemoteDependencies();
    $expected = [
      '00000000-2222-0000-0000-000000000000',
      '00000000-3333-0000-0000-000000000000',
      '00000000-4444-0000-0000-000000000000',
      '00000000-5555-0000-0000-000000000000',
      '00000000-7777-0000-0000-000000000000',
    ];
    $this
      ->assertEquals($expected, $dependencies);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubEntityDependencyTest::$container protected property The dependency injection container.
ContentHubEntityDependencyTest::$entity private property A Content Hub Entity.
ContentHubEntityDependencyTest::$moduleHandler protected property Returns the module handler.
ContentHubEntityDependencyTest::setUp protected function Overrides UnitTestCase::setUp
ContentHubEntityDependencyTest::testDependencyChain public function Test Dependency Chain.
ContentHubEntityDependencyTest::testGetRawEntity public function Test getRawEntity method.
ContentHubEntityDependencyTest::testGetRelationship public function Test for getRelationship() method.
ContentHubEntityDependencyTest::testGetRemoteDependencies public function Test getRemoteDependencies method.
ContentHubEntityDependencyTest::testGetUuidAndType public function Test for getUuid() and getEntityType() method.
ContentHubEntityDependencyTest::testIsEntityDependent public function Test for isEntityDependent() method.
ContentHubEntityDependencyTest::testParenthood public function Test Dependency Chain.
ContentHubEntityDependencyTest::testSetAuthor public function Test setAuthor method.
ContentHubEntityDependencyTest::testSetStatus public function Test setStatus method.
ContentHubEntityTrait::createContentHubEntity private function Creates a Content Hub Entity for testing purposes.
ContentHubEntityTrait::createMultilanguageContentHubEntity private function Creates a Multilanguage Content Hub Entity for testing purposes.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.