Change to tdd_blog
This commit is contained in:
parent
5be880e496
commit
ccaa426a38
18
README.md
18
README.md
|
@ -15,8 +15,8 @@ This module will be used to demonstrate how to take a test-driven approach to
|
||||||
develop a module to the following acceptance criteria:
|
develop a module to the following acceptance criteria:
|
||||||
|
|
||||||
- As a site visitor
|
- As a site visitor
|
||||||
- I want to see a list of all published pages at `/pages`
|
- I want to see a list of all published blog posts at `/blog`
|
||||||
- Ordered alphabetically by title
|
- Ordered by post date, with the newest posts first
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ Within your Drupal 8 site:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd modules
|
cd modules
|
||||||
git clone git@github.com:opdavies/drupal-module-tdd-dublin.git tdd_dublin
|
git clone git@github.com:opdavies/drupal-module-tdd-blog.git tdd_blog
|
||||||
```
|
```
|
||||||
|
|
||||||
## Running the Tests
|
## Running the Tests
|
||||||
|
@ -37,19 +37,19 @@ Because of autoloading, you will either need to be inside Drupal's `core` subdir
|
||||||
, or add `-c core` to the PHPUnit command when running the tests for them to execute successfully.
|
, or add `-c core` to the PHPUnit command when running the tests for them to execute successfully.
|
||||||
|
|
||||||
This also assumes that the module is within a `modules/custom` directory and
|
This also assumes that the module is within a `modules/custom` directory and
|
||||||
named `tdd_dublin` as per the repository name.
|
named `tdd_blog` as per the repository name.
|
||||||
|
|
||||||
```
|
```
|
||||||
vendor/bin/phpunit -c core modules/custom/tdd_dublin
|
vendor/bin/phpunit -c core modules/custom/tdd_blog
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use PHPUnit's `--filter` option to specify a single test method to run,
|
You can use PHPUnit's `--filter` option to specify a single test method to run,
|
||||||
rather than all of the tests within the module. For example:
|
rather than all of the tests within the module. For example:
|
||||||
|
|
||||||
```
|
```
|
||||||
vendor/bin/phpunit -c core modules/custom/tdd_dublin --filter=testOnlyPublishedPagesAreShown
|
vendor/bin/phpunit -c core modules/custom/tdd_blog --filter=testOnlyPublishedPagesAreShown
|
||||||
```
|
```
|
||||||
|
|
||||||
[0]: https://www.oliverdavi.es/talks/tdd-test-driven-drupal
|
[0]: https://www.oliverdavies.uk/talks/tdd-test-driven-drupal
|
||||||
[1]: https://github.com/opdavies/tdd_dublin/commits/HEAD
|
[1]: https://github.com/opdavies/drupal-module-tdd-blog/commits/HEAD
|
||||||
[2]: https://github.com/opdavies/tdd_dublin/tags
|
[2]: https://github.com/opdavies/drupal-module-tdd-blog/tags
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"name": "drupal/tdd_dublin",
|
"name": "drupal/tdd_blog",
|
||||||
"type": "drupal-custom-module"
|
"type": "drupal-custom-module"
|
||||||
}
|
}
|
||||||
|
|
10
config/install/node.type.article.yml
Normal file
10
config/install/node.type.article.yml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
langcode: en
|
||||||
|
status: true
|
||||||
|
dependencies: { }
|
||||||
|
name: 'Basic page'
|
||||||
|
type: page
|
||||||
|
description: 'Use <em>basic pages</em> for your static content, such as an ''About us'' page.'
|
||||||
|
help: ''
|
||||||
|
new_revision: true
|
||||||
|
preview_mode: 1
|
||||||
|
display_submitted: false
|
|
@ -2,12 +2,12 @@ langcode: en
|
||||||
status: true
|
status: true
|
||||||
dependencies:
|
dependencies:
|
||||||
config:
|
config:
|
||||||
- node.type.page
|
- node.type.article
|
||||||
module:
|
module:
|
||||||
- node
|
- node
|
||||||
- user
|
- user
|
||||||
id: pages
|
id: blog
|
||||||
label: pages
|
label: Blog
|
||||||
module: views
|
module: views
|
||||||
description: ''
|
description: ''
|
||||||
tag: ''
|
tag: ''
|
||||||
|
@ -147,7 +147,7 @@ display:
|
||||||
admin_label: ''
|
admin_label: ''
|
||||||
operator: in
|
operator: in
|
||||||
value:
|
value:
|
||||||
page: page
|
article: article
|
||||||
group: 1
|
group: 1
|
||||||
exposed: false
|
exposed: false
|
||||||
expose:
|
expose:
|
||||||
|
@ -179,10 +179,10 @@ display:
|
||||||
entity_field: type
|
entity_field: type
|
||||||
plugin_id: bundle
|
plugin_id: bundle
|
||||||
sorts:
|
sorts:
|
||||||
title:
|
created:
|
||||||
id: title
|
id: created
|
||||||
table: node_field_data
|
table: node_field_data
|
||||||
field: title
|
field: created
|
||||||
relationship: none
|
relationship: none
|
||||||
group_type: group
|
group_type: group
|
||||||
admin_label: ''
|
admin_label: ''
|
||||||
|
@ -190,9 +190,10 @@ display:
|
||||||
exposed: false
|
exposed: false
|
||||||
expose:
|
expose:
|
||||||
label: ''
|
label: ''
|
||||||
|
granularity: second
|
||||||
entity_type: node
|
entity_type: node
|
||||||
entity_field: title
|
entity_field: created
|
||||||
plugin_id: standard
|
plugin_id: date
|
||||||
header: { }
|
header: { }
|
||||||
footer: { }
|
footer: { }
|
||||||
empty: { }
|
empty: { }
|
||||||
|
@ -215,7 +216,7 @@ display:
|
||||||
position: 1
|
position: 1
|
||||||
display_options:
|
display_options:
|
||||||
display_extenders: { }
|
display_extenders: { }
|
||||||
path: pages
|
path: blog
|
||||||
cache_metadata:
|
cache_metadata:
|
||||||
max-age: -1
|
max-age: -1
|
||||||
contexts:
|
contexts:
|
|
@ -1,4 +1,4 @@
|
||||||
name: 'TDD Dublin'
|
name: 'TDD Blog'
|
||||||
description: 'A demo module for DrupalCamp Dublin to show test driven module development.'
|
description: 'A demo module for DrupalCamp Dublin to show test driven module development.'
|
||||||
core: 8.x
|
core: 8.x
|
||||||
type: module
|
type: module
|
17
tests/src/Functional/PageListTest.php
Normal file
17
tests/src/Functional/PageListTest.php
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\Tests\tdd_blog\Functional;
|
||||||
|
|
||||||
|
use Drupal\Tests\BrowserTestBase;
|
||||||
|
|
||||||
|
class PageListTest extends BrowserTestBase {
|
||||||
|
|
||||||
|
protected static $modules = ['tdd_blog'];
|
||||||
|
|
||||||
|
public function testBlogPageExists() {
|
||||||
|
$this->drupalGet('blog');
|
||||||
|
|
||||||
|
$this->assertSession()->statusCodeEquals(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Drupal\Tests\tdd_dublin\Kernel;
|
namespace Drupal\Tests\tdd_blog\Kernel;
|
||||||
|
|
||||||
|
use Drupal\Core\Datetime\DrupalDateTime;
|
||||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||||
use Drupal\node\Entity\Node;
|
|
||||||
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||||
use Drupal\views\ResultRow;
|
use Drupal\views\ResultRow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group tdd_dublin
|
* @group tdd_blog
|
||||||
*/
|
*/
|
||||||
class PageListTest extends EntityKernelTestBase {
|
class PageListTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ class PageListTest extends EntityKernelTestBase {
|
||||||
*/
|
*/
|
||||||
public static $modules = [
|
public static $modules = [
|
||||||
'node',
|
'node',
|
||||||
'tdd_dublin',
|
'tdd_blog',
|
||||||
'views',
|
'views',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ class PageListTest extends EntityKernelTestBase {
|
||||||
$this->installEntitySchema('node');
|
$this->installEntitySchema('node');
|
||||||
$this->installEntitySchema('user');
|
$this->installEntitySchema('user');
|
||||||
|
|
||||||
$this->installConfig(['filter', 'tdd_dublin']);
|
$this->installConfig(['filter', 'tdd_blog']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,58 +41,59 @@ class PageListTest extends EntityKernelTestBase {
|
||||||
* Ensure that only published pages are returned by the view. Unpublished
|
* Ensure that only published pages are returned by the view. Unpublished
|
||||||
* pages or content of different types should not be shown.
|
* pages or content of different types should not be shown.
|
||||||
*/
|
*/
|
||||||
public function testOnlyPublishedPagesAreShown() {
|
public function testOnlyPublishedArticlesAreShown() {
|
||||||
// This is a published page, so it should be visible.
|
// This is a published article, so it should be visible.
|
||||||
$this->createNode(['type' => 'page', 'status' => TRUE]);
|
$this->createNode(['type' => 'page', 'status' => TRUE]);
|
||||||
|
|
||||||
// This is an article, so it should not be visible.
|
// This is a page, so it should not be visible.
|
||||||
$this->createNode(['type' => 'article']);
|
$this->createNode(['type' => 'article']);
|
||||||
|
|
||||||
// This page is not published, so it should not be visible.
|
// This article is not published, so it should not be visible.
|
||||||
$this->createNode(['type' => 'page', 'status' => FALSE]);
|
$this->createNode(['type' => 'article', 'status' => FALSE]);
|
||||||
|
|
||||||
// Rather than testing the rendered HTML, we are going to load the view
|
// Rather than testing the rendered HTML, we are going to load the view
|
||||||
// results programmatically and run assertions against the data it returns.
|
// results programmatically and run assertions against the data it returns.
|
||||||
// This makes it easier to test certain scenarios, and ensures that the
|
// 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
|
// test is future-proofed and won't fail at a later date due to a change in
|
||||||
// the presentation code.
|
// the presentation code.
|
||||||
$nids = array_map(function (ResultRow $result) {
|
$nids = $this->getViewResults();
|
||||||
return $result->_entity->id();
|
|
||||||
}, views_get_view_result('pages'));
|
|
||||||
|
|
||||||
// Only node 1 matches the criteria of being a published page, so only that
|
// 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
|
// node ID should be being returned from the view. assertEquals() can be
|
||||||
// used to compare the expected result to what is being returned.
|
// used to compare the expected result to what is being returned.
|
||||||
$this->assertEquals([1], $nids);
|
$this->assertEquals([2], $nids);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that the results are ordered by title.
|
* Ensure that the results are ordered by title.
|
||||||
*/
|
*/
|
||||||
public function testResultsAreOrderedAlphabetically() {
|
public function testArticlesAreOrderedByDate() {
|
||||||
// Create a number of nodes with different titles, specifying the title for
|
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 day'))->getTimestamp()]);
|
||||||
// each. These are intentionally not in alphabetical order so that when the
|
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 month'))->getTimestamp()]);
|
||||||
// assertion is written for the results to be in the expected order, it
|
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+3 days'))->getTimestamp()]);
|
||||||
// will fail, rather than them being in the expected order based on the
|
$this->createNode(['type' => 'article', 'created' => (new DrupalDateTime('+1 hour'))->getTimestamp()]);
|
||||||
// default sort criteria based on the created timestamp.
|
|
||||||
//
|
|
||||||
// Also, the titles are added explicitly so that the assertion can be
|
|
||||||
// written against the expected order based on these titles. If they
|
|
||||||
// weren't added, each title would be automatically generated so the
|
|
||||||
// expected order would not be known beforehand.
|
|
||||||
$this->createNode(['title' => 'Page A']);
|
|
||||||
$this->createNode(['title' => 'Page D']);
|
|
||||||
$this->createNode(['title' => 'Page C']);
|
|
||||||
$this->createNode(['title' => 'Page B']);
|
|
||||||
|
|
||||||
// Get the result data from the view.
|
// Get the result data from the view.
|
||||||
$nids = array_map(function (ResultRow $result) {
|
$nids = $this->getViewResults();
|
||||||
return $result->_entity->id();
|
|
||||||
}, views_get_view_result('pages'));
|
|
||||||
|
|
||||||
// Compare the expected order based on the titles defined above to the
|
// Compare the expected order based on the titles defined above to the
|
||||||
// ordered results from the view.
|
// ordered results from the view.
|
||||||
$this->assertEquals([1, 4, 3, 2], $nids);
|
$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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue