View source
<?php
namespace Drupal\Tests\rest_views\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormState;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\rest_views\Normalizer\DataNormalizer;
use Drupal\rest_views\Normalizer\RenderNormalizer;
use Drupal\rest_views\Plugin\views\field\EntityFieldExport;
use Drupal\rest_views\SerializedData;
use Drupal\serialization\Encoder\JsonEncoder;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\Serializer\Serializer;
class EntityFieldExportTest extends UnitTestCase {
private $handler;
private $serializer;
protected function setUp() : void {
parent::setUp();
$entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$entityFieldManager = $this
->createMock(EntityFieldManagerInterface::class);
$entityRepository = $this
->createMock(EntityRepositoryInterface::class);
$formatterPluginManager = $this
->getMockBuilder(FormatterPluginManager::class)
->disableOriginalConstructor()
->getMock();
$fieldTypePluginManager = $this
->createMock(FieldTypePluginManagerInterface::class);
$languageManager = $this
->createMock(LanguageManagerInterface::class);
$renderer = $this
->createMock(RendererInterface::class);
$container = new ContainerBuilder();
$translation = $this
->createMock(TranslationInterface::class);
$translation
->method('translateString')
->willReturnCallback(static function (TranslatableMarkup $string) {
return $string
->getUntranslatedString();
});
$container
->set('string_translation', $translation);
\Drupal::setContainer($container);
$this->handler = $this
->getMockBuilder(EntityFieldExport::class)
->setConstructorArgs([
[],
NULL,
[
'entity_type' => 'node',
'field_name' => 'title',
],
$entityTypeManager,
$formatterPluginManager,
$fieldTypePluginManager,
$languageManager,
$renderer,
$entityRepository,
$entityFieldManager,
])
->setMethods([
'getFieldDefinition',
])
->getMock();
$fieldDefinition = $this
->createMock(BaseFieldDefinition::class);
$fieldDefinition
->method('getFieldStorageDefinition')
->willReturn($fieldDefinition);
$fieldDefinition
->method('getColumns')
->willReturn([]);
$this->handler
->method('getFieldDefinition')
->willReturn($fieldDefinition);
$entityFieldManager
->method('getFieldStorageDefinitions')
->with('node')
->willReturn([
'title' => $fieldDefinition,
]);
$view = $this
->getMockBuilder(ViewExecutable::class)
->disableOriginalConstructor()
->getMock();
$view->display_handler = $this
->getMockBuilder(DisplayPluginBase::class)
->disableOriginalConstructor()
->getMock();
$this->handler
->init($view, $view->display_handler);
$this->serializer = new Serializer([
new DataNormalizer(),
new RenderNormalizer($renderer),
], [
new JsonEncoder(),
]);
}
public function testSettings() : void {
$options = $this->handler
->defineOptions();
$this
->assertArrayNotHasKey('multi_type', $options);
$this
->assertArrayNotHasKey('separator', $options);
$form = [];
$this->handler
->multiple_options_form($form, new FormState());
$this
->assertArrayNotHasKey('multi_type', $form);
$this
->assertArrayNotHasKey('separator', $form);
}
public function testRenderItems(array $items, array $expected) : void {
$this->handler->multiple = FALSE;
$result = $this->handler
->renderItems($items);
$json = $this->serializer
->serialize($result, 'json');
$expected_json = $this->serializer
->serialize($expected[0], 'json');
$this
->assertEquals($expected_json, $json);
$this->handler->multiple = TRUE;
$result = $this->handler
->renderItems($items);
$json = $this->serializer
->serialize($result, 'json');
$expected_json = $this->serializer
->serialize($expected, 'json');
$this
->assertEquals($expected_json, $json);
}
public function providerItems() : array {
$data[] = [
'items' => [
'Lorem',
'ipsum',
'dolor',
'sit',
'amet',
],
'expected' => [
'Lorem',
'ipsum',
'dolor',
'sit',
'amet',
],
];
$data[] = [
'items' => [
new SerializedData([
'lorem' => 'ipsum',
]),
new SerializedData([
'dolor' => TRUE,
]),
new SerializedData([
'amet' => 42,
]),
],
'expected' => [
[
'lorem' => 'ipsum',
],
[
'dolor' => TRUE,
],
[
'amet' => 42,
],
],
];
return $data;
}
}