summaryrefslogtreecommitdiffstats
path: root/core/modules/jsonld/lib/Drupal/jsonld/Tests/RdfSchemaSerializationTest.php
blob: eacf9dffa79b3139fc946b09bf5f48647d194e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
 * @file
 * Contains RdfSchemaSerializationTest.
 */

namespace Drupal\jsonld\Tests;

use Drupal\jsonld\JsonldRdfSchemaNormalizer;
use Drupal\jsonld\JsonldEncoder;
use Drupal\rdf\SiteSchema\BundleSchema;
use Drupal\rdf\SiteSchema\SiteSchema;
use Drupal\simpletest\DrupalUnitTestBase;
use Symfony\Component\Serializer\Serializer;

class RdfSchemaSerializationTest extends DrupalUnitTestBase {

  public static $modules = array('system');

  public static function getInfo() {
    return array(
      'name' => 'Site schema JSON-LD serialization',
      'description' => 'Tests the JSON-LD serialization of the RDF site schema.',
      'group' => 'JSON-LD',
    );
  }

  /**
   * Tests the serialization of site schemas.
   */
  function testSchemaSerialization() {
    // url() requires the {url_alias} table.
    $this->installSchema('system', 'url_alias');

    $entity_type = $bundle = 'entity_test';

    // Set up the bundle schema for the entity_test bundle.
    $schema = new SiteSchema(SiteSchema::CONTENT_DEPLOYMENT);
    $bundle_schema = $schema->bundle($entity_type, $bundle);
    // Set up the serializer.
    $setup_helper = new JsonldTestSetupHelper();
    $normalizer = new JsonldRdfSchemaNormalizer($setup_helper->getSiteSchemaManager(), $setup_helper->getRdfMappingManager());
    $serializer = new Serializer(array($normalizer), array(new JsonldEncoder()));

    $serialized = $serializer->serialize($bundle_schema, 'jsonld');
    $decoded = json_decode($serialized);
    $parsed_term = $decoded[0];

    $this->assertEqual($parsed_term->{'@id'}, $bundle_schema->getUri(), 'JSON-LD for schema term uses correct @id.');
    $this->assertEqual($parsed_term->{'@type'}, 'http://www.w3.org/2000/01/rdf-schema#class', 'JSON-LD for schema term uses correct @type.');
    // The @id and @type should be placed in the beginning of the array.
    $array_keys = array_keys((array) $parsed_term);
    $this->assertEqual(array('@id', '@type'), array_slice($array_keys, 0, 2), 'JSON-LD keywords are placed before other properties.');
    $this->assertTrue(isset($parsed_term->{'http://www.w3.org/2000/01/rdf-schema#isDefinedBy'}), 'Other properties of the term are included.');
  }
}