drupal-module-tdd-blog/tests/src/Kernel/PageListTest.php

100 lines
3 KiB
PHP
Raw Normal View History

2017-11-21 00:47:45 +00:00
<?php
2018-09-07 18:33:14 +01:00
namespace Drupal\Tests\tdd_blog\Kernel;
2017-11-21 00:47:45 +00:00
2018-09-07 18:33:14 +01:00
use Drupal\Core\Datetime\DrupalDateTime;
2018-04-19 14:16:57 +01:00
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
2018-04-02 21:29:19 +01:00
use Drupal\views\ResultRow;
2017-11-21 00:47:45 +00:00
/**
2018-09-07 18:33:14 +01:00
* @group tdd_blog
2017-11-21 00:47:45 +00:00
*/
2018-04-19 14:16:57 +01:00
class PageListTest extends EntityKernelTestBase {
2017-11-21 00:47:45 +00:00
use NodeCreationTrait;
2017-11-21 00:47:45 +00:00
/**
* {@inheritdoc}
*/
2018-04-19 14:16:57 +01:00
public static $modules = [
2017-11-21 00:47:45 +00:00
'node',
2018-09-07 18:33:14 +01:00
'tdd_blog',
2017-11-21 00:47:45 +00:00
'views',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('user');
2018-09-07 18:33:14 +01:00
$this->installConfig(['filter', 'tdd_blog']);
2017-11-21 00:47:45 +00:00
}
/**
* Ensure that only the correct nodes are returned.
*
* Ensure that only published pages are returned by the view. Unpublished
* pages or content of different types should not be shown.
*/
2018-09-07 18:33:14 +01:00
public function testOnlyPublishedArticlesAreShown() {
// This is a published article, so it should be visible.
2018-04-20 00:10:28 +01:00
$this->createNode(['type' => 'page', 'status' => TRUE]);
2017-11-21 00:47:45 +00:00
2018-09-07 18:33:14 +01:00
// This is a page, so it should not be visible.
$this->createNode(['type' => 'article']);
2017-11-21 00:47:45 +00:00
2018-09-07 18:33:14 +01:00
// This article is not published, so it should not be visible.
$this->createNode(['type' => 'article', 'status' => FALSE]);
2017-11-21 00:47:45 +00:00
// Rather than testing the rendered HTML, we are going to load the view
// results programmatically and run assertions against the data it returns.
// This makes it easier to test certain scenarios, and ensures that the
// test is future-proofed and won't fail at a later date due to a change in
// the presentation code.
2018-09-07 18:33:14 +01:00
$nids = $this->getViewResults();
2017-11-21 00:47:45 +00:00
// Only node 1 matches the criteria of being a published page, so only that
// node ID should be being returned from the view. assertEquals() can be
// used to compare the expected result to what is being returned.
2018-09-07 18:33:14 +01:00
$this->assertEquals([2], $nids);
2017-11-21 00:47:45 +00:00
}
/**
* Ensure that the results are ordered by title.
*/
2018-09-07 18:33:14 +01:00
public function testArticlesAreOrderedByDate() {
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 day'))->getTimestamp()]);
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 month'))->getTimestamp()]);
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+3 days'))->getTimestamp()]);
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 hour'))->getTimestamp()]);
2017-11-21 00:47:45 +00:00
// Get the result data from the view.
2018-09-07 18:33:14 +01:00
$nids = $this->getViewResults();
2017-11-21 00:47:45 +00:00
// Compare the expected order based on the titles defined above to the
// ordered results from the view.
2018-09-07 18:33:14 +01:00
$this->assertEquals([4, 1, 3, 2], $nids);
}
/**
* Load the view and get the results.
*
* @param string $view
* (optional) The name of the view. Defaults to 'blog'.
*
* @return array
* An array of returned entity IDs.
*/
private function getViewResults($view = 'blog') {
return array_map(function (ResultRow $result) {
return $result->_entity->id();
}, views_get_view_result($view));
2017-11-21 00:47:45 +00:00
}
}