class TestRevisionHandler in Feeds Paragraphs 8
@group Feeds Paragraphs @coversDefaultClass \Drupal\feeds_para_mapper\RevisionHandler
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\feeds_para_mapper\Unit\FpmTestBase
- class \Drupal\Tests\feeds_para_mapper\Unit\TestRevisionHandler uses Common
- class \Drupal\Tests\feeds_para_mapper\Unit\FpmTestBase
Expanded class hierarchy of TestRevisionHandler
File
- tests/
src/ Unit/ TestRevisionHandler.php, line 18
Namespace
Drupal\Tests\feeds_para_mapper\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Common:: |
function | Returns the target class. | ||
Common:: |
public | function | @inheritdoc | |
Common:: |
public | function | @inheritdoc | |
Common:: |
function | Returns the target type. | ||
FpmTestBase:: |
protected | property | The entity helper instance. | |
FpmTestBase:: |
protected | property | The mocked feed type instance. | |
FpmTestBase:: |
protected | property | The field helper instance. | |
FpmTestBase:: |
protected | property | The mocked paragraphs and node fields. | |
FpmTestBase:: |
protected | property | The target mock | |
FpmTestBase:: |
protected | property | The messenger mock | |
FpmTestBase:: |
protected | property | The mocked node. | |
FpmTestBase:: |
protected | property | The services to mock. | |
FpmTestBase:: |
protected | property | The target object. | |
FpmTestBase:: |
protected | property | The wrapper instance. | |
FpmTestBase:: |
protected | function | Adds services to the container. | |
FpmTestBase:: |
protected | function | Generates plugin definitions array (text plugin for now). | |
FpmTestBase:: |
protected | function | Returns a mocked feed entity. | |
FpmTestBase:: |
protected | function | Creates feed type entity. | |
FpmTestBase:: |
protected | function | Generates a mapper object. | |
FpmTestBase:: |
private | function | Mocks the messenger service. | |
FpmTestBase:: |
protected | function | Calls a protected method on an object. | |
FpmTestBase:: |
private | function | Creates FeedsPluginManager instance. | |
FpmTestBase:: |
private | function | Mocks an entity processor instance. | |
FpmTestBase:: |
protected | function | Returns a protected, private property of an object. | |
FpmTestBase:: |
private | function | Instantiates and returns a WrapperTarget object. | |
FpmTestBase:: |
protected | function | Updates a protected, private property of an object. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
TestRevisionHandler:: |
protected | property | ||
TestRevisionHandler:: |
protected | property | ||
TestRevisionHandler:: |
protected | function |
@inheritdoc Overrides FpmTestBase:: |
|
TestRevisionHandler:: |
public | function | @covers ::checkUpdates | |
TestRevisionHandler:: |
public | function | @covers ::cleanUp | |
TestRevisionHandler:: |
public | function | @covers ::__construct | |
TestRevisionHandler:: |
public | function | @covers ::createRevision | |
TestRevisionHandler:: |
public | function | @covers ::handle | |
TestRevisionHandler:: |
public | function | @covers ::removeUnused | |
TestRevisionHandler:: |
public | function | @covers ::updateParentRevision | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |