You are here

class TestRevisionHandler in Feeds Paragraphs 8

@group Feeds Paragraphs @coversDefaultClass \Drupal\feeds_para_mapper\RevisionHandler

Hierarchy

Expanded class hierarchy of TestRevisionHandler

File

tests/src/Unit/TestRevisionHandler.php, line 18

Namespace

Drupal\Tests\feeds_para_mapper\Unit
View source
class TestRevisionHandler extends FpmTestBase {
  use Common;

  /**
   * @var string
   */
  protected $class;

  /**
   * @var string
   */
  protected $type;
  protected function setUp() {
    $this->class = Text::class;
    $this->type = "text";
    parent::setUp();
    $this
      ->addServices($this->services);
  }

  /**
   * @covers ::__construct
   */
  public function testConstruct() {

    // Get mock, without the constructor being called
    $mock = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->getMock();
    $reflectedClass = new \ReflectionClass(RevisionHandler::class);
    $constructor = $reflectedClass
      ->getConstructor();

    // Force the constructor to throw error:
    // now call the constructor
    $importer = $this
      ->prophesize(Importer::class);
    $constructor
      ->invoke($mock, $this->messenger
      ->reveal(), $importer
      ->reveal());
    $props = $reflectedClass
      ->getProperties();
    $initialized = array(
      'messenger',
      'importer',
    );
    foreach ($props as $prop) {
      if (in_array($prop
        ->getName(), $initialized)) {
        $prop
          ->setAccessible(true);
        $val = $prop
          ->getValue($mock);
        self::assertNotEmpty($val);
      }
    }
  }

  /**
   * @covers ::handle
   */
  public function testHandle() {
    $field = end($this->fields)
      ->reveal();
    $info = $this
      ->getTargetInfo();
    $field
      ->set('target_info', $info);
    $fpm_targets = array();
    $fpm_targets[$field
      ->getName()] = $field;
    $node = $this->node
      ->reveal();
    $node->fpm_targets = $fpm_targets;
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'checkUpdates',
      'cleanUp',
    ])
      ->getMock();
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('checkUpdates');
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('cleanUp');
    $revHandler
      ->handle($node);
  }

  /**
   * @covers ::checkUpdates
   */
  public function testCheckUpdates() {
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'createRevision',
    ])
      ->getMock();
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('createRevision');
    $method = $this
      ->getMethod($revHandler, 'checkUpdates');
    $paragraph = end($this->entityHelper->paragraphs);
    $paragraph
      ->isNew()
      ->willReturn(false);
    $method
      ->invokeArgs($revHandler, array(
      array(
        $paragraph
          ->reveal(),
      ),
    ));
  }

  /**
   * @covers ::createRevision
   */
  public function testCreateRevision() {
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods(array(
      'updateParentRevision',
    ))
      ->getMock();
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('updateParentRevision')
      ->with($this
      ->isInstanceOf(Paragraph::class));
    $method = $this
      ->getMethod($revHandler, 'createRevision');
    $paragraph = end($this->entityHelper->paragraphs);
    $bool = Argument::type('bool');
    $paragraph
      ->setNewRevision($bool)
      ->willReturn(null);
    $paragraph
      ->isDefaultRevision($bool)
      ->willReturn(null);
    $method
      ->invoke($revHandler, $paragraph
      ->reveal());
    $paragraph
      ->setNewRevision($bool)
      ->shouldHaveBeenCalled();
    $paragraph
      ->isDefaultRevision($bool)
      ->shouldHaveBeenCalled();
    $paragraph
      ->save()
      ->shouldHaveBeenCalled();
  }

  /**
   * @covers ::updateParentRevision
   */
  public function testUpdateParentRevision() {
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->getMock();
    $method = $this
      ->getMethod($revHandler, 'updateParentRevision');
    $rev_id = 1;
    $frst = $this->entityHelper->paragraphs[1];
    $scnd = $this->entityHelper->paragraphs[2];
    $scnd
      ->getParentEntity()
      ->willReturn($frst
      ->reveal());
    $scnd
      ->updateLoadedRevisionId()
      ->will(function () use (&$rev_id) {
      $rev_id = 2;
      return $this
        ->reveal();
    });
    $scnd
      ->getRevisionId()
      ->will(function () use (&$rev_id) {
      return $rev_id;
    });
    $scndObj = $scnd
      ->reveal();
    $method
      ->invoke($revHandler, $scndObj);
    $frst
      ->save()
      ->shouldHaveBeenCalled();
    $parent = $scndObj
      ->get('parent_field_name')
      ->getValue()[0]['value'];
    $value = $frst
      ->reveal()
      ->get($parent)
      ->getValue()[0];
    self::assertArrayHasKey('target_revision_id', $value);
    self::assertSame(2, $value['target_revision_id']);
  }

  /**
   * @covers ::cleanUp
   */
  public function testCleanUp() {
    $paragraphs = $this->entityHelper->paragraphs;
    $paragraph = $paragraphs[2];

    // Mock RevisionHandler:
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods(array(
      'removeUnused',
    ))
      ->getMock();
    $arr = $this
      ->isType('array');
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('removeUnused')
      ->with($arr, $arr, $this
      ->isInstanceOf(FieldConfigInterface::class));

    // Mock Importer:
    $importer = $this
      ->getMockBuilder(Importer::class)
      ->disableOriginalConstructor()
      ->getMock();
    $importer
      ->expects($this
      ->atLeastOnce())
      ->method('loadTarget')
      ->with($this
      ->isInstanceOf(EntityInterface::class), $this
      ->isInstanceOf(FieldConfigInterface::class))
      ->willReturn(array(
      $paragraph
        ->reveal(),
      $paragraph
        ->reveal(),
    ));
    $this
      ->updateProperty(RevisionHandler::class, $revHandler, 'importer', $importer);
    $this
      ->updateProperty(RevisionHandler::class, $revHandler, 'entity', $this->node
      ->reveal());
    $field = end($this->fieldHelper->fields)
      ->reveal();
    $info = $this
      ->getTargetInfo();
    $info->paragraphs = array(
      $paragraph
        ->reveal(),
    );
    $field
      ->set('target_info', $info);

    // And call the method:
    $method = $this
      ->getMethod($revHandler, 'cleanUp');
    $method
      ->invoke($revHandler, array(
      $field,
    ));
  }

  /**
   * @covers ::removeUnused
   */
  public function testRemoveUnused() {

    // Mock RevisionHandler:
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods(array(
      'createRevision',
    ))
      ->getMock();
    $revHandler
      ->expects($this
      ->never())
      ->method('createRevision')
      ->with($this
      ->isInstanceOf(Paragraph::class));
    $paragraphs = $this->entityHelper->paragraphs;

    // Add additional field to test in common fields removal functionality:
    $parProph = $paragraphs[2];
    $anotherValue = array(
      array(
        'value' => 'Test value',
      ),
    );
    $itemList = $this
      ->prophesize(EntityReferenceRevisionsFieldItemList::class);
    $itemList
      ->getValue()
      ->willReturn($anotherValue);
    $parProph
      ->get('another_field')
      ->willReturn($itemList
      ->reveal());
    $paragraph = $parProph
      ->reveal();
    $used_entities = array(
      $paragraph,
    );
    $attached = array(
      $paragraph,
      $paragraph,
    );
    $field = end($this->fields)
      ->reveal();
    $info = $this
      ->getTargetInfo();
    $info->paragraphs = array(
      $paragraph,
    );
    $info->in_common = array(
      array(
        'name' => 'another_field',
      ),
    );
    $field
      ->set('target_info', $info);
    $method = $this
      ->getMethod(RevisionHandler::class, 'removeUnused');
    $method
      ->invoke($revHandler, $used_entities, $attached, $field);

    // Test with no in common fields:
    $info->in_common = array();
    $field
      ->set('target_info', $info);

    // Mock RevisionHandler to expect a call to createRevision method:
    $revHandler = $this
      ->getMockBuilder(RevisionHandler::class)
      ->disableOriginalConstructor()
      ->setMethods(array(
      'createRevision',
    ))
      ->getMock();
    $revHandler
      ->expects($this
      ->atLeastOnce())
      ->method('createRevision')
      ->with($this
      ->isInstanceOf(Paragraph::class));
    $method
      ->invoke($revHandler, $used_entities, $attached, $field);
    $parent = $paragraph
      ->getParentEntity();
    $parent_field = $paragraph
      ->get('parent_field_name')
      ->getValue()[0]['value'];
    $parentValue = $parent
      ->get($parent_field)
      ->getValue();
    self::assertTrue(!isset($parentValue['target_id']), 'The parent field value is cleaned up');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Common::getClass function Returns the target class.
Common::getInstanceMock public function @inheritdoc
Common::getTargetInfo public function @inheritdoc
Common::getType function Returns the target type.
FpmTestBase::$entityHelper protected property The entity helper instance.
FpmTestBase::$feedType protected property The mocked feed type instance.
FpmTestBase::$fieldHelper protected property The field helper instance.
FpmTestBase::$fields protected property The mocked paragraphs and node fields.
FpmTestBase::$instanceMock protected property The target mock
FpmTestBase::$messenger protected property The messenger mock
FpmTestBase::$node protected property The mocked node.
FpmTestBase::$services protected property The services to mock.
FpmTestBase::$target protected property The target object.
FpmTestBase::$wrapperTarget protected property The wrapper instance.
FpmTestBase::addServices protected function Adds services to the container.
FpmTestBase::generatePluginDefinitions protected function Generates plugin definitions array (text plugin for now).
FpmTestBase::getFeedMock protected function Returns a mocked feed entity.
FpmTestBase::getFeedTypeMock protected function Creates feed type entity.
FpmTestBase::getMapperObject protected function Generates a mapper object.
FpmTestBase::getMessengerMock private function Mocks the messenger service.
FpmTestBase::getMethod protected function Calls a protected method on an object.
FpmTestBase::getPluginManagerMock private function Creates FeedsPluginManager instance.
FpmTestBase::getProcessorMock private function Mocks an entity processor instance.
FpmTestBase::getProperty protected function Returns a protected, private property of an object.
FpmTestBase::initWrapper private function Instantiates and returns a WrapperTarget object.
FpmTestBase::updateProperty protected function Updates a protected, private property of an object.
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.
TestRevisionHandler::$class protected property
TestRevisionHandler::$type protected property
TestRevisionHandler::setUp protected function @inheritdoc Overrides FpmTestBase::setUp
TestRevisionHandler::testCheckUpdates public function @covers ::checkUpdates
TestRevisionHandler::testCleanUp public function @covers ::cleanUp
TestRevisionHandler::testConstruct public function @covers ::__construct
TestRevisionHandler::testCreateRevision public function @covers ::createRevision
TestRevisionHandler::testHandle public function @covers ::handle
TestRevisionHandler::testRemoveUnused public function @covers ::removeUnused
TestRevisionHandler::testUpdateParentRevision public function @covers ::updateParentRevision
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.