annotate core/modules/rdf/tests/src/Kernel/CrudTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\rdf\Kernel;
Chris@0 4
Chris@0 5 use Drupal\KernelTests\KernelTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the RDF mapping CRUD functions.
Chris@0 9 *
Chris@0 10 * @group rdf
Chris@0 11 */
Chris@0 12 class CrudTest extends KernelTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Modules to enable.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 public static $modules = ['entity_test', 'rdf', 'system'];
Chris@0 20
Chris@0 21 /**
Chris@0 22 * @var string
Chris@0 23 */
Chris@0 24 protected $prefix;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * @var string
Chris@0 28 */
Chris@0 29 protected $entityType;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * @var string
Chris@0 33 */
Chris@0 34 protected $bundle;
Chris@0 35
Chris@0 36 protected function setUp() {
Chris@0 37 parent::setUp();
Chris@0 38 $this->prefix = 'rdf.mapping';
Chris@0 39 $this->entityType = $this->bundle = 'entity_test';
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Tests creation of RDF mapping.
Chris@0 44 */
Chris@0 45 public function testMappingCreation() {
Chris@0 46 $mapping_config_name = "{$this->prefix}.{$this->entityType}.{$this->bundle}";
Chris@0 47
Chris@0 48 // Save bundle mapping config.
Chris@0 49 rdf_get_mapping($this->entityType, $this->bundle)->save();
Chris@0 50 // Test that config file was saved.
Chris@0 51 $mapping_config = \Drupal::configFactory()->listAll('rdf.mapping.');
Chris@0 52 $this->assertTrue(in_array($mapping_config_name, $mapping_config), 'Rdf mapping config saved.');
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Test the handling of bundle mappings.
Chris@0 57 */
Chris@0 58 public function testBundleMapping() {
Chris@0 59 // Test that the bundle mapping can be saved.
Chris@0 60 $types = ['sioc:Post', 'foaf:Document'];
Chris@0 61 rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 62 ->setBundleMapping(['types' => $types])
Chris@0 63 ->save();
Chris@0 64 $bundle_mapping = rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 65 ->getBundleMapping();
Chris@0 66 $this->assertEqual($types, $bundle_mapping['types'], 'Bundle mapping saved.');
Chris@0 67
Chris@0 68 // Test that the bundle mapping can be edited.
Chris@0 69 $types = ['schema:BlogPosting'];
Chris@0 70 rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 71 ->setBundleMapping(['types' => $types])
Chris@0 72 ->save();
Chris@0 73 $bundle_mapping = rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 74 ->getBundleMapping();
Chris@0 75 $this->assertEqual($types, $bundle_mapping['types'], 'Bundle mapping updated.');
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Test the handling of field mappings.
Chris@0 80 */
Chris@0 81 public function testFieldMapping() {
Chris@0 82 $field_name = 'created';
Chris@0 83
Chris@0 84 // Test that the field mapping can be saved.
Chris@0 85 $mapping = [
Chris@0 86 'properties' => ['dc:created'],
Chris@0 87 'datatype' => 'xsd:dateTime',
Chris@0 88 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
Chris@0 89 ];
Chris@0 90 rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 91 ->setFieldMapping($field_name, $mapping)
Chris@0 92 ->save();
Chris@0 93 $field_mapping = rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 94 ->getFieldMapping($field_name);
Chris@0 95 $this->assertEqual($mapping, $field_mapping, 'Field mapping saved.');
Chris@0 96
Chris@0 97 // Test that the field mapping can be edited.
Chris@0 98 $mapping = [
Chris@0 99 'properties' => ['dc:date'],
Chris@0 100 'datatype' => 'foo:bar',
Chris@0 101 'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
Chris@0 102 ];
Chris@0 103 rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 104 ->setFieldMapping($field_name, $mapping)
Chris@0 105 ->save();
Chris@0 106 $field_mapping = rdf_get_mapping($this->entityType, $this->bundle)
Chris@0 107 ->getFieldMapping($field_name);
Chris@0 108 $this->assertEqual($mapping, $field_mapping, 'Field mapping updated.');
Chris@0 109 }
Chris@0 110
Chris@0 111 }