View source
<?php
namespace Drupal\Tests\rest_views\Unit;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManagerInterface;
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() {
parent::setUp();
$entityManager = $this
->getMock(EntityManagerInterface::class);
$formatterPluginManager = $this
->getMockBuilder(FormatterPluginManager::class)
->disableOriginalConstructor()
->getMock();
$fieldTypePluginManager = $this
->getMock(FieldTypePluginManagerInterface::class);
$languageManager = $this
->getMock(LanguageManagerInterface::class);
$renderer = $this
->getMock(RendererInterface::class);
$container = new ContainerBuilder();
$translation = $this
->getMock(TranslationInterface::class);
$translation
->method('translateString')
->willReturnCallback(static function (TranslatableMarkup $string) {
return $string
->getUntranslatedString();
});
$container
->set('string_translation', $translation);
$container
->set('entity.repository', $entityManager);
$container
->set('entity_field.manager', $entityManager);
Drupal::setContainer($container);
$this->handler = $this
->getMockBuilder(EntityFieldExport::class)
->setConstructorArgs([
[],
NULL,
[
'entity_type' => 'node',
'field_name' => 'title',
],
$entityManager,
$formatterPluginManager,
$fieldTypePluginManager,
$languageManager,
$renderer,
])
->setMethods([
'getFieldDefinition',
])
->getMock();
$fieldDefinition = $this
->getMock(BaseFieldDefinition::class);
$fieldDefinition
->method('getFieldStorageDefinition')
->willReturn($fieldDefinition);
$fieldDefinition
->method('getColumns')
->willReturn([]);
$this->handler
->method('getFieldDefinition')
->willReturn($fieldDefinition);
$entityManager
->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() {
$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) {
$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() {
$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;
}
}