DrupalJsonldNormalizerTest.php 5.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Definition of Drupal\jsonld\Tests\DrupalJsonldNormalizerTest.
 */

namespace Drupal\jsonld\Tests;

use Drupal\config\Tests\ConfigEntityTest;
use Drupal\Core\Language\Language;
12
13
14
use Drupal\jsonld\JsonldEntityNormalizer;
use Drupal\jsonld\JsonldEntityReferenceNormalizer;
use Drupal\jsonld\JsonldFieldItemNormalizer;
15
use Drupal\jsonld\Tests\JsonldNormalizerTestBase;
16
use Symfony\Component\Serializer\Serializer;
17
18
19
20
21

/**
 * Test the vendor specific JSON-LD normalizer.
 */
class DrupalJsonldNormalizerTest extends JsonldNormalizerTestBase {
22
23
24
25
26
27

  /**
   * The format being tested.
   */
  protected static $format = 'drupal_jsonld';

28
  /**
29
   * The Normalizers to be tested.
30
   */
31
  protected $normalizers;
32
33
34

  public static function getInfo() {
    return array(
35
      'name' => 'vnd.drupal.ld+json Normalization',
36
37
38
39
40
41
42
43
44
45
46
      'description' => "Test Drupal's vendor specific JSON-LD normalizer.",
      'group' => 'JSON-LD',
    );
  }

  /**
   * Add the normalizer to be tested.
   */
  function setUp() {
    parent::setUp();

47
48
49
50
51
52
53
    $this->normalizers = array(
      'entityreference' => new JsonldEntityReferenceNormalizer(),
      'field_item' => new JsonldFieldItemNormalizer(),
      'entity' => new JsonldEntityNormalizer(),
    );
    $serializer = new Serializer($this->normalizers);
    $this->normalizers['entity']->setSerializer($serializer);
54
55
56
57
58
59
  }

  /**
   * Tests the supportsNormalization function.
   */
  public function testSupportsNormalization() {
60
    $format = static::$format;
61
62
    $supportedEntity = entity_create('entity_test', array());
    $unsupportedEntity = new ConfigEntityTest();
63
64
65
66
67
68
69
    $field = $supportedEntity->get('uuid');
    $entityreferenceField = $supportedEntity->get('user_id');

    // Supported entity.
    $this->assertTrue($this->normalizers['entity']->supportsNormalization($supportedEntity, static::$format), "Entity normalization is supported for $format on content entities.");
    // Unsupported entity.
    $this->assertFalse($this->normalizers['entity']->supportsNormalization($unsupportedEntity, static::$format), "Normalization is not supported for other entity types.");
70

71
72
73
74
    // Field item.
    $this->assertTrue($this->normalizers['field_item']->supportsNormalization($field->offsetGet(0), static::$format), "Field item normalization is supported for $format.");
    // Entity reference field item.
    $this->assertTrue($this->normalizers['entityreference']->supportsNormalization($entityreferenceField->offsetGet(0), static::$format), "Entity reference field item normalization is supported for $format.");
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  }

  /**
   * Tests the normalize function.
   */
  public function testNormalize() {
    // Add German as a language.
    $language = new Language(array(
      'langcode' => 'de',
      'name' => 'Deutsch',
    ));
    language_save($language);

    // Create a German entity.
    $values = array(
      'langcode' => 'de',
      'name' => $this->randomName(),
      'user_id' => $GLOBALS['user']->uid,
      'field_test_text' => array(
        'value' => $this->randomName(),
        'format' => 'full_html',
      ),
    );
    // Array of translated values.
    $translationValues = array(
      'name' => $this->randomName(),
    );

    $entity = entity_create('entity_test', $values);
    $entity->save();
    // Add an English value for name property.
    $entity->getTranslation('en')->set('name', array(0 => array('value' => $translationValues['name'])));

    $expectedArray = array(
      '@id' => $this->getEntityId($entity),
      'uuid' => array(
        'und' => array(
          array(
113
            'value' => $entity->uuid(),
114
115
116
117
118
119
          ),
        ),
      ),
      'user_id' => array(
        'de' => array(
          array(
120
            '@id' => url('user/' . $values['user_id'], array('absolute' => TRUE)),
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
          ),
        ),
      ),
      'name' => array(
        'de' => array(
          array(
            'value' => $values['name'],
          ),
        ),
        'en' => array(
          array(
            'value' => $translationValues['name'],
          ),
        ),
      ),
      'field_test_text' => array(
        'und' => array(
          array(
            'value' => $values['field_test_text']['value'],
            'format' => $values['field_test_text']['format'],
          ),
        ),
      ),
    );

146
    $normalized = $this->normalizers['entity']->normalize($entity, static::$format);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    // Test ordering. The @context and @id properties should always be first.
    $keys = array_keys($normalized);
    $this->assertEqual($keys[0], '@id', '@id and @context attributes placed correctly.');
    // Test @id value.
    $this->assertEqual($normalized['@id'], $expectedArray['@id'], '@id uses correct value.');
    // Test non-translatable field.
    $this->assertEqual($normalized['uuid'], $expectedArray['uuid'], 'Non-translatable fields are nested correctly.');
    // Test single-language translatable.
    $this->assertEqual($normalized['user_id'], $expectedArray['user_id'], 'Translatable field with single language value is nested correctly.');
    // Test multi-language translatable.
    $this->assertEqual($normalized['name'], $expectedArray['name'], 'Translatable field with multiple language values is nested correctly.');
    // Test multi-property untranslatable field.
    $this->assertEqual($normalized['field_test_text'], $expectedArray['field_test_text'], 'Field with properties is nested correctly.');
  }

}