View source
<?php
namespace Drupal\Tests\rdf\Functional;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\comment\Entity\Comment;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\rdf\Traits\RdfParsingTrait;
class StandardProfileTest extends BrowserTestBase {
use RdfParsingTrait;
protected $defaultTheme = 'classy';
public $profile = 'standard';
protected $baseUri;
protected $adminUser;
protected $webUser;
protected $term;
protected $image;
protected $article;
protected $articleComment;
protected $page;
protected $imageUri;
protected $termUri;
protected $articleUri;
protected $pageUri;
protected $authorUri;
protected $articleCommentUri;
protected $commenterUri;
protected function setUp() {
parent::setUp();
$this->baseUri = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString();
$this->adminUser = $this
->drupalCreateUser([
'administer content types',
'administer comments',
'access comments',
'access content',
]);
$this->webUser = $this
->drupalCreateUser([
'access comments',
'post comments',
'skip comment approval',
'access content',
]);
$this
->drupalLogin($this->adminUser);
$this->term = Term::create([
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => 'tags',
]);
$this->term
->save();
\Drupal::service('file_system')
->copy($this->root . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
$this->image
->save();
$article_settings = [
'type' => 'article',
'promote' => NodeInterface::PROMOTED,
'field_image' => [
[
'target_id' => $this->image
->id(),
],
],
'field_tags' => [
[
'target_id' => $this->term
->id(),
],
],
];
$this->article = $this
->drupalCreateNode($article_settings);
$this
->drupalCreateNode([
'type' => 'article',
'promote' => NodeInterface::PROMOTED,
]);
$this->articleComment = $this
->saveComment($this->article
->id(), $this->webUser
->id(), NULL, 0);
$this->page = $this
->drupalCreateNode([
'type' => 'page',
]);
$image_file = $this->article
->get('field_image')->entity;
$this->imageUri = ImageStyle::load('large')
->buildUrl($image_file
->getFileUri());
$this->termUri = $this->term
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this->articleUri = $this->article
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this->pageUri = $this->page
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this->authorUri = $this->adminUser
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this->articleCommentUri = $this->articleComment
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this->commenterUri = $this->webUser
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$this
->drupalLogout();
}
public function testRdfaOutput() {
$this
->doFrontPageRdfaTests();
$this
->doArticleRdfaTests();
$this
->doPageRdfaTests();
$this
->doUserRdfaTests();
$this
->doTermRdfaTests();
}
protected function doFrontPageRdfaTests() {
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertEqual(2, $this
->getElementByRdfTypeCount(Url::fromRoute('<front>'), $this->baseUri, 'http://schema.org/Article'), 'Two articles found on front page.');
$expected_value = [
'type' => 'literal',
'value' => 'UserComments:1',
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleUri, 'http://schema.org/interactionCount', $expected_value), 'Teaser comment count was found (schema:interactionCount).');
$this
->assertRdfaCommonNodeProperties($this->article, "Teaser");
$this
->assertRdfaArticleProperties("Teaser");
$expected_value = [
'type' => 'uri',
'value' => $this->imageUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleUri, 'http://schema.org/image', $expected_value), 'Teaser image was found (schema:image).');
}
protected function doArticleRdfaTests() {
$this
->drupalGet($this->article
->toUrl());
$this
->assertEqual($this
->getElementRdfType($this->article
->toUrl(), $this->baseUri, $this->articleUri), 'schema:Article', 'Article type was found (schema:Article).');
$this
->assertRdfaCommonNodeProperties($this->article, "Article");
$this
->assertRdfaArticleProperties("Article");
$this
->assertRdfaNodeCommentProperties();
$expected_value = [
'type' => 'uri',
'value' => $this->imageUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleUri, 'http://schema.org/image', $expected_value), 'Teaser image was found (schema:image).');
}
protected function doPageRdfaTests() {
$node_type = NodeType::load('page');
$node_type
->setDisplaySubmitted(TRUE);
$node_type
->save();
$this
->assertEqual($this
->getElementRdfType($this->page
->toUrl(), $this->baseUri, $this->pageUri), 'schema:WebPage', 'Page type was found (schema:WebPage).');
$this
->assertRdfaCommonNodeProperties($this->page, "Page");
}
protected function doUserRdfaTests() {
$this
->drupalLogin($this->rootUser);
$this
->assertEqual($this
->getElementRdfType($this->adminUser
->toUrl(), $this->baseUri, $this->authorUri), 'schema:Person', 'User type was found (schema:Person) on user page.');
$expected_value = [
'type' => 'literal',
'value' => $this->adminUser
->label(),
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->authorUri, 'http://schema.org/name', $expected_value), 'User name was found (schema:name) on user page.');
$this
->drupalLogout();
}
protected function doTermRdfaTests() {
$this
->assertEqual($this
->getElementRdfType($this->term
->toUrl(), $this->baseUri, $this->termUri), 'schema:Thing', 'Term type was found (schema:Thing) on term page.');
$expected_value = [
'type' => 'literal',
'value' => $this->term
->getName(),
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->termUri, 'http://schema.org/name', $expected_value), 'Term name was found (schema:name) on term page.');
}
protected function assertRdfaCommonNodeProperties(NodeInterface $node, $message_prefix) {
$this
->drupalGet($node
->toUrl());
$uri = $node
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString();
$expected_value = [
'type' => 'literal',
'value' => $node
->get('title')->value,
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $uri, 'http://schema.org/name', $expected_value), "{$message_prefix} title was found (schema:name).");
$expected_value = [
'type' => 'literal',
'value' => $this->container
->get('date.formatter')
->format($node
->get('created')->value, 'custom', 'c', 'UTC'),
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $uri, 'http://schema.org/dateCreated', $expected_value), "{$message_prefix} created date was found (schema:dateCreated) in teaser.");
$expected_value = [
'type' => 'literal',
'value' => $node
->get('body')->value,
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $uri, 'http://schema.org/text', $expected_value), "{$message_prefix} body was found (schema:text) in teaser.");
$expected_value = [
'type' => 'uri',
'value' => $this->authorUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $uri, 'http://schema.org/author', $expected_value), "{$message_prefix} author was found (schema:author) in teaser.");
$this
->assertEqual($this
->getElementRdfType($node
->toUrl(), $this->baseUri, $this->authorUri), 'schema:Person', '$message_prefix author type was found (schema:Person).');
$expected_value = [
'type' => 'literal',
'value' => $this->adminUser
->label(),
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->authorUri, 'http://schema.org/name', $expected_value), "{$message_prefix} author name was found (schema:name).");
}
protected function assertRdfaArticleProperties($message_prefix) {
$expected_value = [
'type' => 'uri',
'value' => $this->termUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleUri, 'http://schema.org/about', $expected_value), "{$message_prefix} tag was found (schema:about).");
$expected_value = [
'type' => 'literal',
'value' => $this->term
->getName(),
'lang' => 'en',
];
}
protected function assertRdfaNodeCommentProperties() {
$this
->drupalGet($this->article
->toUrl());
$expected_value = [
'type' => 'uri',
'value' => $this->articleCommentUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleUri, 'http://schema.org/comment', $expected_value), "Relationship between node and comment found (schema:comment).");
$this
->assertEqual($this
->getElementRdfType($this->article
->toUrl(), $this->baseUri, $this->articleCommentUri), 'schema:Comment', 'Comment type was found (schema:Comment).');
$expected_value = [
'type' => 'literal',
'value' => $this->articleComment
->get('subject')->value,
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleCommentUri, 'http://schema.org/name', $expected_value), "Article comment title was found (schema:name).");
$expected_value = [
'type' => 'literal',
'value' => $this->container
->get('date.formatter')
->format($this->articleComment
->get('created')->value, 'custom', 'c', 'UTC'),
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleCommentUri, 'http://schema.org/dateCreated', $expected_value), "Article comment created date was found (schema:dateCreated).");
$text = $this->articleComment
->get('comment_body')->value;
$expected_value = [
'type' => 'literal',
'value' => "{$text}\n",
'lang' => 'en',
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleCommentUri, 'http://schema.org/text', $expected_value), "Article comment body was found (schema:text).");
$expected_value = [
'type' => 'uri',
'value' => $this->commenterUri,
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->articleCommentUri, 'http://schema.org/author', $expected_value), "Article comment author was found (schema:author).");
$this
->assertEqual($this
->getElementRdfType($this->article
->toUrl(), $this->baseUri, $this->commenterUri), 'schema:Person', 'Comment author type was found (schema:Person).');
$expected_value = [
'type' => 'literal',
'value' => $this->webUser
->getAccountName(),
];
$this
->assertTrue($this
->hasRdfProperty($this
->getSession()
->getPage()
->getContent(), $this->baseUri, $this->commenterUri, 'http://schema.org/name', $expected_value), "Comment author name was found (schema:name).");
}
protected function saveComment($nid, $uid, $contact = NULL, $pid = 0) {
$values = [
'entity_id' => $nid,
'entity_type' => 'node',
'field_name' => 'comment',
'uid' => $uid,
'pid' => $pid,
'subject' => $this
->randomMachineName(),
'comment_body' => $this
->randomMachineName(),
'status' => 1,
];
if ($contact) {
$values += $contact;
}
$comment = Comment::create($values);
$comment
->save();
return $comment;
}
}