class TestMapper in Feeds Paragraphs 8
@group Feeds Paragraphs @coversDefaultClass \Drupal\feeds_para_mapper\Mapper
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\TestMapper uses Common
- class \Drupal\Tests\feeds_para_mapper\Unit\FpmTestBase
Expanded class hierarchy of TestMapper
File
- tests/
src/ Unit/ TestMapper.php, line 16
Namespace
Drupal\Tests\feeds_para_mapper\UnitView source
class TestMapper extends FpmTestBase {
use Common;
/**
* @var string
*/
protected $class;
/**
* @var string
*/
protected $type;
/**
* @var Mapper
*/
protected $mapper;
/**
* @inheritdoc
*/
protected function setUp() {
$this->class = Text::class;
$this->type = "text";
parent::setUp();
$this
->addServices($this->services);
$this->mapper = $this
->getMapperObject();
}
/**
* @covers ::getTargets
*/
public function testGetTargets() {
$targets = $this->mapper
->getTargets('node', 'product');
$message = "Targets found";
self::assertTrue(count($targets) > 0, $message);
$targets = $this->mapper
->getTargets('test', 'test');
$message = "Returns empty array if there are no targets";
self::assertTrue(is_array($targets) && count($targets) === 0, $message);
}
/**
*
* @covers ::findParagraphsFields
*/
public function testFindParagraphsFields() {
$fields = $this->mapper
->findParagraphsFields('node', 'product');
$message = "Paragraphs fields found";
$isFound = count($fields) && $fields[0]
->getType() === "entity_reference_revisions";
self::assertTrue($isFound, $message);
$fields = $this->mapper
->findParagraphsFields('test', 'test');
$message = "Returns empty array if no paragraphs fields found";
self::assertTrue(is_array($fields) && count($fields) === 0, $message);
}
/**
*
* @covers ::getSubFields
*/
public function testGetSubFields() {
// Test against a paragraph field:
$field = $this->fields[0]
->reveal();
$subFields = $this->mapper
->getSubFields($field);
$targetInfo = $subFields[0]
->get('target_info');
$message = "TargetInfo object is attached to the target field";
self::assertTrue(isset($targetInfo), $message);
$notParagraph = $subFields[0]
->getType() !== "entity_reference_revisions";
$message = "Field type not paragraph";
self::assertTrue($notParagraph, $message);
// Test against non-paragraph field:
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
$subFields = $this->mapper
->getSubFields($field);
$message = "Returns array if no sub-fields found";
self::assertTrue(is_array($subFields) && count($subFields) === 0, $message);
}
/**
*
* @covers ::updateInfo
*/
public function testUpdateInfo() {
// Test against a field with TargetInfo attached:
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
$this->mapper
->updateInfo($field, 'path', array(
'test' => 'test value',
));
$info = $field
->get('target_info');
$message = "TargetInfo property is updated";
self::assertTrue(count($info->path) === 1 && $info->path['test'] === 'test value', $message);
// Test against a field with no TargetInfo attached:
$field
->set('target_info', null);
$this->mapper
->updateInfo($field, 'path', array(
'test' => 'test value 2',
));
$info = $field
->get('target_info');
$message = "TargetInfo property is updated";
self::assertTrue(count($info->path) === 1 && $info->path['test'] === 'test value 2', $message);
// Test with non-existing property:
$res = $this->mapper
->updateInfo($field, 'test', 'test');
$message = "should return false with non-existing property";
self::assertFalse($res, $message);
}
/**
*
* @covers ::getInfo
*/
public function testGetInfo() {
// Test against a field with TargetInfo attached:
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
$info = $this
->getTargetInfo();
$info->plugin = array();
$field
->set('target_info', $info);
$res = $this->mapper
->getInfo($field, 'plugin');
self::assertNotNull($res, "property should exist");
$res = $this->mapper
->getInfo($field, 'test');
self::assertNull($res, "property should not exist");
// Test against a field with no TargetInfo attached:
$field
->set('target_info', null);
$res = $this->mapper
->getInfo($field, 'test');
self::assertNull($res, "Should return null if no TargetInfo attached to the field");
}
/**
* @covers ::setFieldsInCommon
*/
public function testSetFieldsInCommon() {
$conf = new FieldConfig('Bundle two field two', 'bundle_two_field_two', 'text', 4, 1, array(
'handler_settings' => array(),
), array(), 'paragraph', 'bundle_two', 'bundle_one_bundle_two', 3);
$path = array(
array(
'bundle' => 'bundle_one',
'host_field' => 'paragraph_field',
'host_entity' => 'node',
'order' => 0,
),
);
$firstTargetInfo = $this
->getTargetInfo();
$firstTargetInfo->path = $path;
$secondTargetInfo = $this
->getTargetInfo();
$secondTargetInfo->path = $path;
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
$field
->set('target_info', $firstTargetInfo);
$second_field = $this->fieldHelper
->getField($conf)
->reveal();
$second_field
->set('target_info', $secondTargetInfo);
$fields = array(
$second_field,
);
$method = $this
->getMethod(Mapper::class, 'setFieldsInCommon');
$method
->invokeArgs($this->mapper, array(
&$field,
&$fields,
));
$inCommonExists = count($firstTargetInfo->in_common) && count($secondTargetInfo->in_common);
$message = "in common field are added for both fields";
self::assertTrue($inCommonExists, $message);
$first_field_name = $field
->getName();
$second_field_name = $second_field
->getName();
$field_two_has_one = $secondTargetInfo->in_common[0]['name'] === $first_field_name;
self::assertTrue($field_two_has_one, "First field has the second in common");
$field_one_has_two = $firstTargetInfo->in_common[0]['name'] === $second_field_name;
self::assertTrue($field_one_has_two, "Second field has the first in common");
}
/**
*
* @covers ::buildPath
*
*/
public function testBuildPath() {
$method = $this
->getMethod(Mapper::class, 'buildPath');
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
$first_host = array(
'bundle' => 'bundle_one',
'host_field' => 'paragraph_field',
'host_entity' => 'node',
'order' => 0,
);
$path = $method
->invokeArgs($this->mapper, array(
$field,
$first_host,
));
self::assertTrue(is_array($path) && count($path), "Field path exists");
$bundles = $this->fieldHelper->bundles;
$bundles = array_values($bundles);
for ($i = 0; $i < count($bundles); $i++) {
self::assertTrue($path[$i]['bundle'] === $bundles[$i], "bundle exists in the path");
}
}
/**
* @covers ::getMaxValues
*/
public function testGetMaxValues() {
$field = $this->fieldHelper
->getBundleFields('bundle_two')[0]
->reveal();
// Cardinality is -1:
$expected = (int) $field
->getFieldStorageDefinition()
->getCardinality();
$max = $this->mapper
->getMaxValues($field);
self::assertSame($expected, $max);
$max = $this->mapper
->getMaxValues($field, array(
'max_values' => 10,
));
self::assertSame(10, $max);
// Invalid input should return the field cardinality:
$max = $this->mapper
->getMaxValues($field, array(
'max_values' => -2,
));
self::assertSame($expected, $max);
$field
->set('cardinality', '2');
$max = $this->mapper
->getMaxValues($field, array(
'max_values' => -2,
));
self::assertSame(2, $max);
$max = $this->mapper
->getMaxValues($field, array(
'max_values' => 3,
));
self::assertSame(2, $max);
}
}
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. | |
TestMapper:: |
protected | property | ||
TestMapper:: |
protected | property | ||
TestMapper:: |
protected | property | ||
TestMapper:: |
protected | function |
@inheritdoc Overrides FpmTestBase:: |
|
TestMapper:: |
public | function | @covers ::buildPath | |
TestMapper:: |
public | function | @covers ::findParagraphsFields | |
TestMapper:: |
public | function | @covers ::getInfo | |
TestMapper:: |
public | function | @covers ::getMaxValues | |
TestMapper:: |
public | function | @covers ::getSubFields | |
TestMapper:: |
public | function | @covers ::getTargets | |
TestMapper:: |
public | function | @covers ::setFieldsInCommon | |
TestMapper:: |
public | function | @covers ::updateInfo | |
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. |