Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / rdf / tests / src / Kernel / Field / LinkFieldRdfaTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Kernel\Field;
4
5 use Drupal\entity_test\Entity\EntityTest;
6
7 /**
8  * Tests the placement of RDFa in link field formatters.
9  *
10  * @group rdf
11  */
12 class LinkFieldRdfaTest extends FieldRdfaTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected $fieldType = 'link';
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['link', 'text'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->createTestField();
31
32     // Add the mapping.
33     $mapping = rdf_get_mapping('entity_test', 'entity_test');
34     $mapping->setFieldMapping($this->fieldName, [
35       'properties' => ['schema:link'],
36     ])->save();
37
38   }
39
40   /**
41    * Tests all formatters with link to external page.
42    */
43   public function testAllFormattersExternal() {
44     // Set up test values.
45     $this->testValue = 'http://test.me/foo/bar/neque/porro/quisquam/est/qui-dolorem?path=foo/bar/neque/porro/quisquam/est/qui-dolorem';
46     $this->entity = EntityTest::create([]);
47     $this->entity->{$this->fieldName}->uri = $this->testValue;
48
49     // Set up the expected result.
50     $expected_rdf = [
51       'value' => $this->testValue,
52       'type' => 'uri',
53     ];
54
55     $this->runTestAllFormatters($expected_rdf, 'external');
56   }
57
58   /**
59    * Tests all formatters with link to internal page.
60    */
61   public function testAllFormattersInternal() {
62     // Set up test values.
63     $this->testValue = 'admin';
64     $this->entity = EntityTest::create([]);
65     $this->entity->{$this->fieldName}->uri = 'internal:/admin';
66
67     // Set up the expected result.
68     // AssertFormatterRdfa looks for a full path.
69     $expected_rdf = [
70       'value' => $this->uri . '/' . $this->testValue,
71       'type' => 'uri',
72     ];
73
74     $this->runTestAllFormatters($expected_rdf, 'internal');
75   }
76
77   /**
78    * Tests all formatters with link to frontpage.
79    */
80   public function testAllFormattersFront() {
81     // Set up test values.
82     $this->testValue = '/';
83     $this->entity = EntityTest::create([]);
84     $this->entity->{$this->fieldName}->uri = 'internal:/';
85
86     // Set up the expected result.
87     $expected_rdf = [
88       'value' => $this->uri . '/',
89       'type' => 'uri',
90     ];
91
92     $this->runTestAllFormatters($expected_rdf, 'front');
93   }
94
95   /**
96    * Helper function to test all link formatters.
97    */
98   public function runTestAllFormatters($expected_rdf, $type = NULL) {
99
100     // Test the link formatter: trim at 80, no other settings.
101     $formatter = [
102       'type' => 'link',
103       'settings' => [
104         'trim_length' => 80,
105         'url_only' => FALSE,
106         'url_plain' => FALSE,
107         'rel' => '',
108         'target' => '',
109       ],
110     ];
111     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
112
113     // Test the link formatter: trim at 40, nofollow, new window.
114     $formatter = [
115       'type' => 'link',
116       'settings' => [
117         'trim_length' => 40,
118         'url_only' => FALSE,
119         'url_plain' => FALSE,
120         'rel' => 'nofollow',
121         'target' => '_blank',
122       ],
123     ];
124     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
125
126     // Test the link formatter: trim at 40, URL only (not plaintext) nofollow,
127     // new window.
128     $formatter = [
129       'type' => 'link',
130       'settings' => [
131         'trim_length' => 40,
132         'url_only' => TRUE,
133         'url_plain' => FALSE,
134         'rel' => 'nofollow',
135         'target' => '_blank',
136       ],
137     ];
138     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
139
140     // Test the link_separate formatter: trim at 40, nofollow, new window.
141     $formatter = [
142       'type' => 'link_separate',
143       'settings' => [
144         'trim_length' => 40,
145         'rel' => 'nofollow',
146         'target' => '_blank',
147       ],
148     ];
149     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
150
151     // Change the expected value here to literal. When formatted as plaintext
152     // then the RDF is expecting a 'literal' not a 'uri'.
153     $expected_rdf = [
154       'value' => $this->testValue,
155       'type' => 'literal',
156     ];
157     // Test the link formatter: trim at 20, url only (as plaintext.)
158     $formatter = [
159       'type' => 'link',
160       'settings' => [
161         'trim_length' => 20,
162         'url_only' => TRUE,
163         'url_plain' => TRUE,
164         'rel' => '0',
165         'target' => '0',
166       ],
167     ];
168     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
169
170     // Test the link formatter: do not trim, url only (as plaintext.)
171     $formatter = [
172       'type' => 'link',
173       'settings' => [
174         'trim_length' => 0,
175         'url_only' => TRUE,
176         'url_plain' => TRUE,
177         'rel' => '0',
178         'target' => '0',
179       ],
180     ];
181     $this->assertFormatterRdfa($formatter, 'http://schema.org/link', $expected_rdf);
182   }
183
184 }