Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / rdf / tests / src / Functional / CommentAttributesTest.php
1 <?php
2
3 namespace Drupal\Tests\rdf\Functional;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\Tests\comment\Functional\CommentTestBase;
8 use Drupal\user\RoleInterface;
9 use Drupal\comment\Entity\Comment;
10
11 /**
12  * Tests the RDFa markup of comments.
13  *
14  * @group rdf
15  */
16 class CommentAttributesTest extends CommentTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['views', 'node', 'comment', 'rdf'];
24
25   /**
26    * URI of the front page of the Drupal site.
27    *
28    * @var string
29    */
30   protected $baseUri;
31
32   /**
33    * URI of the test node created by CommentTestBase::setUp().
34    *
35    * @var string
36    */
37   protected $nodeUri;
38
39   protected function setUp() {
40     parent::setUp();
41
42     // Enables anonymous user comments.
43     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
44       'access comments' => TRUE,
45       'post comments' => TRUE,
46       'skip comment approval' => TRUE,
47     ]);
48     // Allows anonymous to leave their contact information.
49     $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
50     $this->setCommentPreview(DRUPAL_OPTIONAL);
51     $this->setCommentForm(TRUE);
52     $this->setCommentSubject(TRUE);
53     $this->setCommentSettings('comment_default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
54
55     // Prepares commonly used URIs.
56     $this->baseUri = \Drupal::url('<front>', [], ['absolute' => TRUE]);
57     $this->nodeUri = $this->node->url('canonical', ['absolute' => TRUE]);
58
59     // Set relation between node and comment.
60     $article_mapping = rdf_get_mapping('node', 'article');
61     $comment_count_mapping = [
62       'properties' => ['sioc:num_replies'],
63       'datatype' => 'xsd:integer',
64       'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::rawValue'],
65     ];
66     $article_mapping->setFieldMapping('comment_count', $comment_count_mapping)->save();
67
68     // Save user mapping.
69     $user_mapping = rdf_get_mapping('user', 'user');
70     $username_mapping = [
71       'properties' => ['foaf:name'],
72     ];
73     $user_mapping->setFieldMapping('name', $username_mapping)->save();
74     $user_mapping->setFieldMapping('homepage', ['properties' => ['foaf:page'], 'mapping_type' => 'rel'])->save();
75
76     // Save comment mapping.
77     $mapping = rdf_get_mapping('comment', 'comment');
78     $mapping->setBundleMapping(['types' => ['sioc:Post', 'sioct:Comment']])->save();
79     $field_mappings = [
80       'subject' => [
81         'properties' => ['dc:title'],
82       ],
83       'created' => [
84         'properties' => ['dc:date', 'dc:created'],
85         'datatype' => 'xsd:dateTime',
86         'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
87       ],
88       'changed' => [
89         'properties' => ['dc:modified'],
90         'datatype' => 'xsd:dateTime',
91         'datatype_callback' => ['callable' => 'Drupal\rdf\CommonDataConverter::dateIso8601Value'],
92       ],
93       'comment_body' => [
94         'properties' => ['content:encoded'],
95       ],
96       'pid' => [
97         'properties' => ['sioc:reply_of'],
98         'mapping_type' => 'rel',
99       ],
100       'uid' => [
101         'properties' => ['sioc:has_creator'],
102         'mapping_type' => 'rel',
103       ],
104       'name' => [
105         'properties' => ['foaf:name'],
106       ],
107     ];
108     // Iterate over shared field mappings and save.
109     foreach ($field_mappings as $field_name => $field_mapping) {
110       $mapping->setFieldMapping($field_name, $field_mapping)->save();
111     }
112   }
113
114   /**
115    * Tests the presence of the RDFa markup for the number of comments.
116    */
117   public function testNumberOfCommentsRdfaMarkup() {
118     // Posts 2 comments on behalf of registered user.
119     $this->saveComment($this->node->id(), $this->webUser->id());
120     $this->saveComment($this->node->id(), $this->webUser->id());
121
122     // Tests number of comments in teaser view.
123     $this->drupalLogin($this->webUser);
124     $parser = new \EasyRdf_Parser_Rdfa();
125     $graph = new \EasyRdf_Graph();
126     $parser->parse($graph, $this->drupalGet('node'), 'rdfa', $this->baseUri);
127
128     // Number of comments.
129     $expected_value = [
130       'type' => 'literal',
131       'value' => 2,
132       'datatype' => 'http://www.w3.org/2001/XMLSchema#integer',
133     ];
134     $this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of teaser view (sioc:num_replies).');
135
136     // Tests number of comments in full node view, expected value is the same.
137     $parser = new \EasyRdf_Parser_Rdfa();
138     $graph = new \EasyRdf_Graph();
139     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
140     $this->assertTrue($graph->hasProperty($this->nodeUri, 'http://rdfs.org/sioc/ns#num_replies', $expected_value), 'Number of comments found in RDF output of full node view mode (sioc:num_replies).');
141   }
142
143   /**
144    * Tests comment author link markup has not been broken by RDF.
145    */
146   public function testCommentRdfAuthorMarkup() {
147     // Post a comment as a registered user.
148     $this->saveComment($this->node->id(), $this->webUser->id());
149
150     // Give the user access to view user profiles so the profile link shows up.
151     user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']);
152     $this->drupalLogin($this->webUser);
153
154     // Ensure that the author link still works properly after the author output
155     // is modified by the RDF module.
156     $this->drupalGet('node/' . $this->node->id());
157     $this->assertLink($this->webUser->getUsername());
158     $this->assertLinkByHref('user/' . $this->webUser->id());
159   }
160
161   /**
162    * Tests if RDFa markup for meta information is present in comments.
163    *
164    * Tests presence of RDFa markup for the title, date and author and homepage
165    * on comments from registered and anonymous users.
166    */
167   public function testCommentRdfaMarkup() {
168     // Posts comment #1 on behalf of registered user.
169     $comment1 = $this->saveComment($this->node->id(), $this->webUser->id());
170
171     // Tests comment #1 with access to the user profile.
172     $this->drupalLogin($this->webUser);
173     $parser = new \EasyRdf_Parser_Rdfa();
174     $graph = new \EasyRdf_Graph();
175     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
176     $this->_testBasicCommentRdfaMarkup($graph, $comment1);
177
178     // Tests comment #1 with no access to the user profile (as anonymous user).
179     $this->drupalLogout();
180     $parser = new \EasyRdf_Parser_Rdfa();
181     $graph = new \EasyRdf_Graph();
182     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
183     $this->_testBasicCommentRdfaMarkup($graph, $comment1);
184
185     // Posts comment #2 as anonymous user.
186     $anonymous_user = [];
187     $anonymous_user['name'] = $this->randomMachineName();
188     $anonymous_user['mail'] = 'tester@simpletest.org';
189     $anonymous_user['homepage'] = 'http://example.org/';
190     $comment2 = $this->saveComment($this->node->id(), 0, $anonymous_user);
191
192     // Tests comment #2 as anonymous user.
193     $parser = new \EasyRdf_Parser_Rdfa();
194     $graph = new \EasyRdf_Graph();
195     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
196     $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
197
198     // Tests comment #2 as logged in user.
199     $this->drupalLogin($this->webUser);
200     $parser = new \EasyRdf_Parser_Rdfa();
201     $graph = new \EasyRdf_Graph();
202     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
203     $this->_testBasicCommentRdfaMarkup($graph, $comment2, $anonymous_user);
204   }
205
206   /**
207    * Tests RDF comment replies.
208    */
209   public function testCommentReplyOfRdfaMarkup() {
210     // Posts comment #1 on behalf of registered user.
211     $this->drupalLogin($this->webUser);
212     $comment_1 = $this->saveComment($this->node->id(), $this->webUser->id());
213
214     $comment_1_uri = $comment_1->url('canonical', ['absolute' => TRUE]);
215
216     // Posts a reply to the first comment.
217     $comment_2 = $this->saveComment($this->node->id(), $this->webUser->id(), NULL, $comment_1->id());
218     $comment_2_uri = $comment_2->url('canonical', ['absolute' => TRUE]);
219
220     $parser = new \EasyRdf_Parser_Rdfa();
221     $graph = new \EasyRdf_Graph();
222     $parser->parse($graph, $this->drupalGet('node/' . $this->node->id()), 'rdfa', $this->baseUri);
223
224     // Tests the reply_of relationship of a first level comment.
225     $expected_value = [
226       'type' => 'uri',
227       'value' => $this->nodeUri,
228     ];
229     $this->assertTrue($graph->hasProperty($comment_1_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
230
231     // Tests the reply_of relationship of a second level comment.
232     $expected_value = [
233       'type' => 'uri',
234       'value' => $this->nodeUri,
235     ];
236     $this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its node found in RDF output (sioc:reply_of).');
237     $expected_value = [
238       'type' => 'uri',
239       'value' => $comment_1_uri,
240     ];
241     $this->assertTrue($graph->hasProperty($comment_2_uri, 'http://rdfs.org/sioc/ns#reply_of', $expected_value), 'Comment relation to its parent comment found in RDF output (sioc:reply_of).');
242   }
243
244   /**
245    * Helper function for testCommentRdfaMarkup().
246    *
247    * Tests the current page for basic comment RDFa markup.
248    *
249    * @param $comment
250    *   Comment object.
251    * @param $account
252    *   An array containing information about an anonymous user.
253    */
254   public function _testBasicCommentRdfaMarkup($graph, CommentInterface $comment, $account = []) {
255     $comment_uri = $comment->url('canonical', ['absolute' => TRUE]);
256
257     // Comment type.
258     $expected_value = [
259       'type' => 'uri',
260       'value' => 'http://rdfs.org/sioc/types#Comment',
261     ];
262     $this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioct:Comment).');
263     // Comment type.
264     $expected_value = [
265       'type' => 'uri',
266       'value' => 'http://rdfs.org/sioc/ns#Post',
267     ];
268     $this->assertTrue($graph->hasProperty($comment_uri, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $expected_value), 'Comment type found in RDF output (sioc:Post).');
269
270     // Comment title.
271     $expected_value = [
272       'type' => 'literal',
273       'value' => $comment->getSubject(),
274       'lang' => 'en',
275     ];
276     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/title', $expected_value), 'Comment subject found in RDF output (dc:title).');
277
278     // Comment date.
279     $expected_value = [
280       'type' => 'literal',
281       'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
282       'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
283     ];
284     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/date', $expected_value), 'Comment date found in RDF output (dc:date).');
285     // Comment date.
286     $expected_value = [
287       'type' => 'literal',
288       'value' => format_date($comment->getCreatedTime(), 'custom', 'c', 'UTC'),
289       'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime',
290     ];
291     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/dc/terms/created', $expected_value), 'Comment date found in RDF output (dc:created).');
292
293     // Comment body.
294     $expected_value = [
295       'type' => 'literal',
296       'value' => $comment->comment_body->value . "\n",
297       'lang' => 'en',
298     ];
299     $this->assertTrue($graph->hasProperty($comment_uri, 'http://purl.org/rss/1.0/modules/content/encoded', $expected_value), 'Comment body found in RDF output (content:encoded).');
300
301     // The comment author can be a registered user or an anonymous user.
302     if ($comment->getOwnerId() > 0) {
303       $author_uri = \Drupal::url('entity.user.canonical', ['user' => $comment->getOwnerId()], ['absolute' => TRUE]);
304       // Comment relation to author.
305       $expected_value = [
306         'type' => 'uri',
307         'value' => $author_uri,
308       ];
309       $this->assertTrue($graph->hasProperty($comment_uri, 'http://rdfs.org/sioc/ns#has_creator', $expected_value), 'Comment relation to author found in RDF output (sioc:has_creator).');
310     }
311     else {
312       // The author is expected to be a blank node.
313       $author_uri = $graph->get($comment_uri, '<http://rdfs.org/sioc/ns#has_creator>');
314       if ($author_uri instanceof \EasyRdf_Resource) {
315         $this->assertTrue($author_uri->isBnode(), 'Comment relation to author found in RDF output (sioc:has_creator) and author is blank node.');
316       }
317       else {
318         $this->fail('Comment relation to author found in RDF output (sioc:has_creator).');
319       }
320     }
321
322     // Author name.
323     $name = empty($account["name"]) ? $this->webUser->getUsername() : $account["name"] . " (not verified)";
324     $expected_value = [
325       'type' => 'literal',
326       'value' => $name,
327     ];
328     $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/name', $expected_value), 'Comment author name found in RDF output (foaf:name).');
329
330     // Comment author homepage (only for anonymous authors).
331     if ($comment->getOwnerId() == 0) {
332       $expected_value = [
333         'type' => 'uri',
334         'value' => 'http://example.org/',
335       ];
336       $this->assertTrue($graph->hasProperty($author_uri, 'http://xmlns.com/foaf/0.1/page', $expected_value), 'Comment author link found in RDF output (foaf:page).');
337     }
338   }
339
340   /**
341    * Creates a comment entity.
342    *
343    * @param $nid
344    *   Node id which will hold the comment.
345    * @param $uid
346    *   User id of the author of the comment. Can be NULL if $contact provided.
347    * @param $contact
348    *   Set to NULL for no contact info, TRUE to ignore success checking, and
349    *   array of values to set contact info.
350    * @param $pid
351    *   Comment id of the parent comment in a thread.
352    *
353    * @return \Drupal\comment\Entity\Comment
354    *   The saved comment.
355    */
356   public function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
357     $values = [
358       'entity_id' => $nid,
359       'entity_type' => 'node',
360       'field_name' => 'comment',
361       'uid' => $uid,
362       'pid' => $pid,
363       'subject' => $this->randomMachineName(),
364       'comment_body' => $this->randomMachineName(),
365       'status' => 1,
366     ];
367     if ($contact) {
368       $values += $contact;
369     }
370
371     $comment = Comment::create($values);
372     $comment->save();
373     return $comment;
374   }
375
376 }