comparison core/modules/tour/tests/src/Functional/TourTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\tour\Functional;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\tour\Entity\Tour;
7
8 /**
9 * Tests the functionality of tour tips.
10 *
11 * @group tour
12 */
13 class TourTest extends TourTestBasic {
14
15 /**
16 * Modules to enable.
17 *
18 * @var array
19 */
20 public static $modules = ['block', 'tour', 'locale', 'language', 'tour_test'];
21
22 /**
23 * The permissions required for a logged in user to test tour tips.
24 *
25 * @var array
26 * A list of permissions.
27 */
28 protected $permissions = ['access tour', 'administer languages'];
29
30 /**
31 * Tour tip attributes to be tested. Keyed by the path.
32 *
33 * @var array
34 * An array of tip attributes, keyed by path.
35 */
36 protected $tips = [
37 'tour-test-1' => [],
38 ];
39
40 /**
41 * {@inheritdoc}
42 */
43 protected function setUp() {
44 parent::setUp();
45
46 $this->drupalPlaceBlock('local_actions_block', [
47 'theme' => 'seven',
48 'region' => 'content'
49 ]);
50 }
51
52 /**
53 * Test tour functionality.
54 */
55 public function testTourFunctionality() {
56 // Navigate to tour-test-1 and verify the tour_test_1 tip is found with appropriate classes.
57 $this->drupalGet('tour-test-1');
58
59 // Test the TourTestBase class assertTourTips() method.
60 $tips = [];
61 $tips[] = ['data-id' => 'tour-test-1'];
62 $tips[] = ['data-class' => 'tour-test-5'];
63 $this->assertTourTips($tips);
64 $this->assertTourTips();
65
66 $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./p//a[@href=:href and contains(., :text)]]', [
67 ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
68 ':data_id' => 'tour-test-1',
69 ':href' => \Drupal::url('<front>', [], ['absolute' => TRUE]),
70 ':text' => 'Drupal',
71 ]);
72 $this->assertEqual(count($elements), 1, 'Found Token replacement.');
73
74 $elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('The first tip')");
75 $this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
76
77 $elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
78 $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 2.');
79
80 $elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('La pioggia cade in spagna')");
81 $this->assertNotEqual(count($elements), 1, 'Did not find Italian variant of tip 1.');
82
83 // Ensure that plugins work.
84 $elements = $this->xpath('//img[@src="http://local/image.png"]');
85 $this->assertEqual(count($elements), 1, 'Image plugin tip found.');
86
87 // Navigate to tour-test-2/subpath and verify the tour_test_2 tip is found.
88 $this->drupalGet('tour-test-2/subpath');
89 $elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
90 $this->assertEqual(count($elements), 1, 'Found English variant of tip 2.');
91
92 $elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('The first tip')");
93 $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');
94
95 // Enable Italian language and navigate to it/tour-test1 and verify italian
96 // version of tip is found.
97 ConfigurableLanguage::createFromLangcode('it')->save();
98 $this->drupalGet('it/tour-test-1');
99
100 $elements = $this->cssSelect("li[data-id=tour-test-1] h2:contains('La pioggia cade in spagna')");
101 $this->assertEqual(count($elements), 1, 'Found Italian variant of tip 1.');
102
103 $elements = $this->cssSelect("li[data-id=tour-test-2] h2:contains('The quick brown fox')");
104 $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');
105
106 // Programmatically create a tour for use through the remainder of the test.
107 $tour = Tour::create([
108 'id' => 'tour-entity-create-test-en',
109 'label' => 'Tour test english',
110 'langcode' => 'en',
111 'module' => 'system',
112 'routes' => [
113 ['route_name' => 'tour_test.1'],
114 ],
115 'tips' => [
116 'tour-test-1' => [
117 'id' => 'tour-code-test-1',
118 'plugin' => 'text',
119 'label' => 'The rain in spain',
120 'body' => 'Falls mostly on the plain.',
121 'weight' => '100',
122 'attributes' => [
123 'data-id' => 'tour-code-test-1',
124 ],
125 ],
126 'tour-code-test-2' => [
127 'id' => 'tour-code-test-2',
128 'plugin' => 'image',
129 'label' => 'The awesome image',
130 'url' => 'http://local/image.png',
131 'weight' => 1,
132 'attributes' => [
133 'data-id' => 'tour-code-test-2'
134 ],
135 ],
136 ],
137 ]);
138 $tour->save();
139
140 // Ensure that a tour entity has the expected dependencies based on plugin
141 // providers and the module named in the configuration entity.
142 $dependencies = $tour->calculateDependencies()->getDependencies();
143 $this->assertEqual($dependencies['module'], ['system', 'tour_test']);
144
145 $this->drupalGet('tour-test-1');
146
147 // Load it back from the database and verify storage worked.
148 $entity_save_tip = Tour::load('tour-entity-create-test-en');
149 // Verify that hook_ENTITY_TYPE_load() integration worked.
150 $this->assertEqual($entity_save_tip->loaded, 'Load hooks work');
151 // Verify that hook_ENTITY_TYPE_presave() integration worked.
152 $this->assertEqual($entity_save_tip->label(), 'Tour test english alter');
153
154 // Navigate to tour-test-1 and verify the new tip is found.
155 $this->drupalGet('tour-test-1');
156 $elements = $this->cssSelect("li[data-id=tour-code-test-1] h2:contains('The rain in spain')");
157 $this->assertEqual(count($elements), 1, 'Found the required tip markup for tip 4');
158
159 // Verify that the weight sorting works by ensuring the lower weight item
160 // (tip 4) has the 'End tour' button.
161 $elements = $this->cssSelect("li[data-id=tour-code-test-1][data-text='End tour']");
162 $this->assertEqual(count($elements), 1, 'Found code tip was weighted last and had "End tour".');
163
164 // Test hook_tour_alter().
165 $this->assertText('Altered by hook_tour_tips_alter');
166
167 // Navigate to tour-test-3 and verify the tour_test_1 tip is found with
168 // appropriate classes.
169 $this->drupalGet('tour-test-3/foo');
170 $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', [
171 ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
172 ':data_id' => 'tour-test-1',
173 ':text' => 'The first tip',
174 ]);
175 $this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
176
177 // Navigate to tour-test-3 and verify the tour_test_1 tip is not found with
178 // appropriate classes.
179 $this->drupalGet('tour-test-3/bar');
180 $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', [
181 ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
182 ':data_id' => 'tour-test-1',
183 ':text' => 'The first tip',
184 ]);
185 $this->assertEqual(count($elements), 0, 'Did not find English variant of tip 1.');
186 }
187
188 }