ImportBasicTest.php in Bibliography & Citation 8
File
modules/bibcite_import/tests/src/Kernel/ImportBasicTest.php
View source
<?php
namespace Drupal\Tests\bibcite_import\Kernel;
use Drupal\Core\Entity\EntityInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\bibcite_entity\Entity\Reference;
use Symfony\Component\Yaml\Yaml;
class ImportBasicTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'serialization',
'bibcite',
'bibcite_entity',
'bibcite_import',
'bibcite_bibtex',
'bibcite_ris',
];
protected $formatManager;
protected $serializer;
public function setUp() {
parent::setUp();
$this
->installEntitySchema('bibcite_keyword');
$this
->installEntitySchema('bibcite_contributor');
$this
->installConfig([
'system',
'user',
'serialization',
'bibcite',
'bibcite_import',
'bibcite_bibtex',
'bibcite_ris',
]);
$this->formatManager = $this->container
->get('plugin.manager.bibcite_format');
$this->serializer = $this->container
->get('serializer');
}
public function testAvailableFormats($format) {
$this
->assertTrue($this->formatManager
->hasDefinition($format));
}
public function testReferenceDeserialization($format, $text, $expected_type, $entity_expected_values) {
$entries = $this->serializer
->decode($text, $format);
foreach ($entries as $entry) {
$entity = $this->serializer
->denormalize($entry, Reference::class, $format);
$this
->assertTrue($entity instanceof Reference);
$this
->assertEquals($expected_type, $entity->type->target_id);
$this
->assertEntityValues($entity, $entity_expected_values);
}
}
protected function assertEntityValues(EntityInterface $entity, array $expected_values) {
foreach ($expected_values as $field_name => $expected_value) {
$this
->assertNotEmpty($entity
->get($field_name));
$this
->assertEquals($expected_value, $entity->{$field_name}->value);
}
}
public function importData() {
$yaml_text = file_get_contents(__DIR__ . '/data/ImportBasicTest.data.yml');
return Yaml::parse($yaml_text);
}
}