View source
<?php
namespace Drupal\Tests\extra_field\Kernel;
use Drupal\extra_field_test\Plugin\ExtraField\Display\AllNodeTypesTest;
use Drupal\extra_field_test\Plugin\ExtraField\Display\EmptyFormattedFieldTest;
use Drupal\extra_field_test\Plugin\ExtraField\Display\SingleTextFieldTest;
use Drupal\extra_field_test\Plugin\ExtraField\Display\MultipleItemsFieldTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class ExtraFieldDisplayPluginTest extends KernelTestBase {
use NodeCreationTrait;
public static $modules = [
'extra_field',
'extra_field_test',
'node',
'field',
'user',
'system',
];
protected $displayManager;
protected $node;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this->container
->get('entity_type.manager')
->getStorage('node_type')
->create([
'name' => 'Test',
'title_label' => 'Title',
'type' => 'test',
'create_body' => FALSE,
])
->save();
$this->node = $this->container
->get('entity_type.manager')
->getStorage('node')
->create([
'type' => 'test',
'title' => $this
->randomString(),
]);
}
public function testDisplayPlugin($pluginId, array $output) {
switch ($pluginId) {
case 'all_node_types_test':
$extraField = new AllNodeTypesTest([], $pluginId, []);
break;
}
$pluginOutput = $extraField
->view($this->node);
unset($pluginOutput['#object']);
unset($pluginOutput['#items']);
$this
->assertEquals($output, $pluginOutput);
}
public function displayPluginProvider() {
$info[] = [
'all_node_types_test',
[
'#markup' => 'Output from AllNodeTypesTest',
],
];
return $info;
}
public function testDisplayFormattedPlugin($pluginId, array $output, array $methods) {
switch ($pluginId) {
case 'single_text_test':
$extraField = new SingleTextFieldTest([], $pluginId, []);
break;
case 'empty_formatted_test':
$extraField = new EmptyFormattedFieldTest([], $pluginId, []);
break;
case 'multiple_text_test':
$extraField = new MultipleItemsFieldTest([], $pluginId, []);
break;
}
$pluginOutput = $extraField
->view($this->node);
unset($pluginOutput['#object']);
unset($pluginOutput['#items']);
$pluginMethod = [
'getLabel' => $extraField
->getLabel(),
'getLabelDisplay' => $extraField
->getLabelDisplay(),
'getFieldName' => $extraField
->getFieldName(),
'getFieldType' => $extraField
->getFieldType(),
'isEmpty' => $extraField
->isEmpty(),
'getLangcode' => $extraField
->getLangcode(),
'isTranslatable' => $extraField
->isTranslatable(),
];
$this
->assertEquals($output, $pluginOutput);
$this
->assertEquals($methods, $pluginMethod);
}
public function displayPluginFormattedProvider() {
$info[] = [
'single_text_test',
[
'#theme' => 'field',
'#title' => 'Single text',
'#label_display' => 'inline',
'#view_mode' => '_custom',
'#language' => 'und',
'#field_name' => 'field_single_text',
'#field_type' => 'single_text',
'#field_translatable' => FALSE,
'#entity_type' => 'node',
'#bundle' => 'test',
'#formatter' => 'single_text_test',
'#is_multiple' => FALSE,
'0' => [
'#markup' => 'Output from SingleTextFieldTest',
],
],
[
'getLabel' => 'Single text',
'getLabelDisplay' => 'inline',
'getFieldName' => 'field_single_text',
'getFieldType' => 'single_text',
'isEmpty' => FALSE,
'getLangcode' => 'und',
'isTranslatable' => FALSE,
],
];
$info[] = [
'empty_formatted_test',
[
'#cache' => [
'max-age' => 0,
],
],
[
'getLabel' => 'Empty field',
'getLabelDisplay' => 'inline',
'getFieldName' => 'extra_field_empty_formatted_test',
'getFieldType' => 'extra_field',
'isEmpty' => TRUE,
'getLangcode' => 'und',
'isTranslatable' => FALSE,
],
];
$info[] = [
'multiple_text_test',
[
'#theme' => 'field',
'#title' => '',
'#label_display' => 'hidden',
'#view_mode' => '_custom',
'#language' => 'und',
'#field_name' => 'extra_field_multiple_text_test',
'#field_type' => 'extra_field',
'#field_translatable' => FALSE,
'#entity_type' => 'node',
'#bundle' => 'test',
'#formatter' => 'multiple_text_test',
'#is_multiple' => TRUE,
'#cache' => [],
'#children' => '',
'0' => [
'#markup' => 'Aap',
],
'1' => [
'#markup' => 'Noot',
],
'2' => [
'#markup' => 'Mies',
],
'3' => [
'#markup' => 'Wim',
],
'4' => [
'#markup' => 'Zus',
],
'5' => [
'#markup' => 'Jet',
],
],
[
'getLabel' => '',
'getLabelDisplay' => 'hidden',
'getFieldName' => 'extra_field_multiple_text_test',
'getFieldType' => 'extra_field',
'isEmpty' => FALSE,
'getLangcode' => 'und',
'isTranslatable' => FALSE,
],
];
return $info;
}
}