View source
<?php
namespace Drupal\Tests\json_feed\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class DisplayJsonFeedTest extends ViewTestBase {
const ADMIN_NAME = 'John Appleseed';
public static $modules = [
'block',
'node',
'views',
'json_feed',
'json_feed_test_views',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_display_json_feed',
];
protected $feedPath = 'test-json-feed-display/json';
protected $htmlAllowedAttributes = [
'content_html',
];
protected $feedItemsPerPage = 10;
protected $nodesToCreate = 25;
protected function setUp($import_test_views = TRUE) {
parent::setUp();
ViewTestData::createTestViews(get_class($this), [
'json_feed_test_views',
]);
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
], self::ADMIN_NAME);
$this
->drupalLogin($admin_user);
$this
->drupalCreateContentType([
'type' => 'page',
]);
$node_data = [
'title' => 'This "cool" & "neat" article\'s title',
'body' => [
0 => [
'value' => '<p>A <em>paragraph</em>.</p>',
'format' => filter_default_format(),
],
],
];
for ($i = 0; $i < $this->nodesToCreate; $i++) {
$this
->drupalCreateNode($node_data);
}
}
public function testFeedOutput() {
$json_response = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
$this
->assertResponse(200);
$this
->assertTrue(array_key_exists('version', $json_response), 'JSON Feed version present.');
$this
->assertEqual('https://jsonfeed.org/version/1', $json_response['version'], 'JSON Feed version set correctly.');
$this
->assertTrue(array_key_exists('title', $json_response), 'JSON Feed title present.');
$this
->assertEqual('test_display_json_feed', $json_response['title'], 'JSON Feed title set correctly.');
$this
->assertTrue(array_key_exists('description', $json_response), 'JSON Feed description present.');
$this
->assertEqual('Test feed description.', $json_response['description'], 'JSON Feed description set correctly.');
$this
->assertTrue(array_key_exists('home_page_url', $json_response), 'JSON Feed home_page_url present.');
$this
->assertTrue(array_key_exists('feed_url', $json_response), 'JSON Feed feed_url present.');
$this
->assertTrue(strpos($json_response['feed_url'], $this->feedPath) !== FALSE, 'JSON Feed feed_url set correctly.');
$this
->assertTrue(array_key_exists('favicon', $json_response), 'JSON Feed favicon present.');
$favicon_path = Url::fromUserInput(theme_get_setting('favicon.url'))
->setAbsolute()
->toString();
$this
->assertEqual($favicon_path, $json_response['favicon'], 'JSON Feed favicon set correctly.');
$this
->assertTrue(array_key_exists('expired', $json_response), 'JSON Feed expired attribute present.');
$this
->assertEqual(FALSE, $json_response['expired'], 'JSON Feed expired attribute set to FALSE.');
}
public function testFeedItems() {
$json_response = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
$this
->assertEqual($this
->expectedFirstPageItems(), count($json_response['items']), 'JSON Feed returned ' . $this
->expectedFirstPageItems() . ' items.');
$item = $json_response['items'][0];
$this
->assertTrue(array_key_exists('date_published', $item), 'JSON Feed item date_published attribute present.');
$this
->assertTrue(array_key_exists('date_modified', $item), 'JSON Feed item date_modified attribute present.');
$this
->assertTrue(array_key_exists('tags', $item), 'JSON Feed item tags attribute present.');
}
public function testFeedItemAuthor() {
$json_response = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
$item = $json_response['items'][0];
$this
->assertTrue(array_key_exists('author', $item), 'JSON Feed item author attribute present.');
$author_info = $item['author'];
$this
->assertTrue(array_key_exists('name', $author_info), 'JSON Feed item author name attribute present.');
$this
->assertEqual(self::ADMIN_NAME, $author_info['name'], 'JSON Feed item author name set correctly.');
}
public function testHtmlPresence() {
$json_response = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
array_walk_recursive($json_response, function ($item, $key) {
if (!is_array($item) && !in_array($key, $this->htmlAllowedAttributes)) {
$this
->assertEqual(strip_tags($item), $item, 'JSON Feed item: \'' . $key . '\' does not contain HTML.');
}
});
}
public function testFeedPagnation() {
$feed_content = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
if ($this->feedItemsPerPage < $this->nodesToCreate) {
$this
->assertTrue(array_key_exists('next_url', $feed_content), 'JSON Feed next_url attribute present.');
}
else {
$this
->assertFalse(array_key_exists('next_url', $feed_content), 'JSON Feed next_url attribute not present.');
}
$this
->assertEqual($this
->expectedFirstPageItems(), count($feed_content['items']), 'JSON Feed first page returned ' . $this
->expectedFirstPageItems() . ' items.');
}
public function testFeedLastPage() {
$feed_content = $this
->getFeedLastPage();
$this
->assertFalse(array_key_exists('next_url', $feed_content), 'JSON Feed next_url attribute not present on last page.');
$expectedLastPageItemsCount = $this->nodesToCreate % $this->feedItemsPerPage;
$this
->assertEqual($expectedLastPageItemsCount, count($feed_content['items']), 'JSON Feed last page returned ' . $expectedLastPageItemsCount . ' items.');
}
protected function getFeedNextPage(array $feed_content) {
if (empty($feed_content['next_url'])) {
return NULL;
}
return Json::decode($this
->drupalGet($feed_content['next_url'], [
'query' => [
'_format' => 'json',
],
]));
}
protected function getFeedLastPage() {
$feed_content = Json::decode($this
->drupalGet($this->feedPath, [
'query' => [
'_format' => 'json',
],
]));
if (empty($feed_content['next_url'])) {
return $feed_content;
}
while (!empty($feed_content['next_url'])) {
$feed_content = $this
->getFeedNextPage($feed_content);
}
return $feed_content;
}
protected function expectedFirstPageItems() {
return min($this->feedItemsPerPage, $this->nodesToCreate);
}
}