Skip to content

Commit 25970cd

Browse files
committed
Expanding testing_example for tutorial support
1 parent ded47b3 commit 25970cd

File tree

6 files changed

+254
-1
lines changed

6 files changed

+254
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Drupal\testing_example\Controller;
4+
5+
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6+
use Drupal\Core\StringTranslation\StringTranslationTrait;
7+
use Drupal\Core\StringTranslation\TranslationInterface;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
10+
/**
11+
* A highly-contrived controller class used to demonstrate unit testing.
12+
*/
13+
class ContrivedController implements ContainerInjectionInterface {
14+
15+
use StringTranslationTrait;
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public static function create(ContainerInterface $container) {
21+
return new static(
22+
$container->get('string_translation')
23+
);
24+
}
25+
26+
/**
27+
* Construct a new controller.
28+
*
29+
* @param Drupal\Core\StringTranslation\TranslationInterface $translation
30+
* The translation service.
31+
*/
32+
public function __construct(TranslationInterface $translation) {
33+
$this->setStringTranslation($translation);
34+
}
35+
36+
/**
37+
* A controller method which displays a sum in terms of hands.
38+
*
39+
* @param int $first
40+
* A parameter to the controller path.
41+
* @param int $second
42+
* A parameter to the controller path.
43+
* @return string[]
44+
* A markup array.
45+
*/
46+
public function displayAddedNumbers($first, $second) {
47+
return [
48+
'#markup' => '<p>' . $this->handCount($first, $second) . '</p>',
49+
];
50+
}
51+
52+
/**
53+
* Generate a message based on how many hands are needed to count the sum.
54+
*
55+
* @param int $first
56+
* First parameter.
57+
* @param int $second
58+
* Second parameter.
59+
*
60+
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
61+
* The translated message.
62+
*/
63+
protected function handCount($first, $second) {
64+
$sum = abs($this->add((int)$first, (int)$second));
65+
if ($sum <= 5) {
66+
$message = $this->t('I can count these on one hand.');
67+
}
68+
else if ($sum <= 10) {
69+
$message = $this->t('I need two hands to count these.');
70+
}
71+
else {
72+
$message = $this->t("That's just too many numbers to count.");
73+
}
74+
return $message;
75+
}
76+
77+
/**
78+
* Add two numbers.
79+
*
80+
* @param int $first
81+
* The first parameter.
82+
* @param int $second
83+
* The second parameter.
84+
*
85+
* @return int
86+
* The sum of the two parameters.
87+
*/
88+
protected function add($first, $second) {
89+
return $first + $second;
90+
}
91+
92+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
testing_example.description:
22
title: Testing Example
33
route_name: testing_example.description
4+
expanded: TRUE
5+
testing_example.sum_in_hands:
6+
title: Sum In Hands
7+
route_name: testing_example.sum_in_hands
8+
parent: testing_example.description
+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
# testing_example only has one route. It is to a page explaining the module.
1+
# This route is to a page explaining the module.
22
testing_example.description:
33
path: 'examples/testing-example'
44
defaults:
55
_controller: '\Drupal\testing_example\Controller\TestingExampleController::description'
66
requirements:
77
_permission: 'access content'
8+
# This route displays a sum of two numbers in terms of how many hands are
9+
# required to count it.
10+
testing_example.sum_in_hands:
11+
path: 'examples/testing-example/sum-in-hands/{first}/{second}'
12+
defaults:
13+
_controller: '\Drupal\testing_example\Controller\ContrivedController::displayAddedNumbers'
14+
first: 23
15+
second: 77
16+
requirements:
17+
_permission: 'access content'
18+
first: '^[0-9]+'
19+
second: '^[0-9]+'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Drupal\Tests\testing_example\Functional;
4+
5+
use Drupal\Tests\BrowserTestBase;
6+
7+
/**
8+
* Tests core menu behavior.
9+
*
10+
* This test uses BrowserTestBase to set up a fixture site so that we can test
11+
* for the existence of the 'Add content' link normally provided by the Standard
12+
* profile.
13+
*
14+
* The 'bonus points' are a reward for the effort to explicitly build up the
15+
* dependencies, rather than simply using the Standard profile.
16+
*
17+
* This test is meant to support a Drupalize.me tutorial.
18+
*
19+
* @group testing_example
20+
*
21+
* @see Drupal\Tests\testing_example\Functional\FrontPageLinkTest
22+
*/
23+
class FrontPageLinkBonusPointsTest extends BrowserTestBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public static $modules = ['node', 'block', 'user'];
29+
30+
/**
31+
* Our node type.
32+
*
33+
* @var \Drupal\node\Entity\NodeType
34+
*/
35+
protected $contentType;
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function setUp() {
41+
// Always call the parent setUp().
42+
parent::setUp();
43+
// Add the Tools menu block, as provided by the Block module.
44+
$this->placeBlock('system_menu_block:tools');
45+
// Add a content type.
46+
$this->contentType = $this->createContentType();
47+
}
48+
49+
/**
50+
* Tests for the existence of a default menu item on the home page.
51+
*
52+
* We'll open the home page and look for the Tools menu link called 'Add
53+
* content.'
54+
*/
55+
public function testAddContentMenuItem() {
56+
// Step 1: Log in a user who can add content.
57+
$this->drupalLogin(
58+
$this->drupalCreateUser([
59+
'create ' . $this->contentType->id() . ' content',
60+
])
61+
);
62+
63+
// Step 2: Visit the home path.
64+
$this->drupalGet($this->buildUrl(''));
65+
// Step 3: Look on the page for the 'Add content' link.
66+
$this->assertSession()->linkExists('Add content');
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Drupal\Tests\testing_example\Functional;
4+
5+
use Drupal\Tests\BrowserTestBase;
6+
7+
/**
8+
* Tests core menu behavior.
9+
*
10+
* This test is meant to support a Drupalize.me tutorial.
11+
*
12+
* @group testing_example
13+
*
14+
* @see Drupal\Tests\testing_example\Functional\FrontPageLinkBonusPointsTest
15+
*/
16+
class FrontPageLinkTest extends BrowserTestBase {
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected $profile = 'standard';
22+
23+
/**
24+
* Tests for the existence of a default menu item on the home page.
25+
*
26+
* We'll open the home page and look for the Tools menu link called 'Add
27+
* content.'
28+
*/
29+
public function testAddContentMenuItem() {
30+
// Step 1: Log in a user who can add content.
31+
$this->drupalLogin(
32+
$this->drupalCreateUser([
33+
'create article content',
34+
])
35+
);
36+
37+
// Step 2: Visit the home path.
38+
$this->drupalGet($this->buildUrl(''));
39+
// Step 3: Look on the page for the 'Add content' link.
40+
$this->assertSession()->linkExists('Add content');
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Drupal\Tests\testing_example\Unit\Controller;
4+
5+
use Drupal\Tests\UnitTestCase;
6+
use Drupal\testing_example\Controller\ContrivedController;
7+
8+
/**
9+
* @group testing_example
10+
*/
11+
class ContrivedControllerTest extends UnitTestCase {
12+
13+
public function provideTestAdd() {
14+
return [
15+
[4, 2, 2],
16+
[0, NULL, '']
17+
];
18+
}
19+
20+
/**
21+
* @dataProvider provideTestAdd
22+
*/
23+
public function testAdd($expected, $first, $second) {
24+
$controller = $this->getMockBuilder(ContrivedController::class)
25+
->disableOriginalConstructor()
26+
->getMock();
27+
$ref_add = new \ReflectionMethod($controller, 'add');
28+
$ref_add->setAccessible(TRUE);
29+
$this->assertEquals($expected, $ref_add->invokeArgs($controller, [$first, $second]));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)