3 namespace Drupal\tour\Tests;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\tour\Entity\Tour;
9 * Tests the functionality of tour tips.
13 class TourTest extends TourTestBasic {
20 public static $modules = ['block', 'tour', 'locale', 'language', 'tour_test'];
23 * The permissions required for a logged in user to test tour tips.
26 * A list of permissions.
28 protected $permissions = ['access tour', 'administer languages'];
31 * Tour tip attributes to be tested. Keyed by the path.
34 * An array of tip attributes, keyed by path.
43 protected function setUp() {
46 $this->drupalPlaceBlock('local_actions_block', [
53 * Test tour functionality.
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');
59 // Test the TourTestBase class assertTourTips() method.
61 $tips[] = ['data-id' => 'tour-test-1'];
62 $tips[] = ['data-class' => 'tour-test-5'];
63 $this->assertTourTips($tips);
64 $this->assertTourTips();
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]),
72 $this->assertEqual(count($elements), 1, 'Found Token replacement.');
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.');
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.');
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.');
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.');
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.');
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.');
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');
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.');
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.');
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',
111 'module' => 'system',
113 ['route_name' => 'tour_test.1'],
117 'id' => 'tour-code-test-1',
119 'label' => 'The rain in spain',
120 'body' => 'Falls mostly on the plain.',
123 'data-id' => 'tour-code-test-1',
126 'tour-code-test-2' => [
127 'id' => 'tour-code-test-2',
129 'label' => 'The awesome image',
130 'url' => 'http://local/image.png',
133 'data-id' => 'tour-code-test-2'
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']);
145 $this->drupalGet('tour-test-1');
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');
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');
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".');
164 // Test hook_tour_alter().
165 $this->assertText('Altered by hook_tour_tips_alter');
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',
175 $this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
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',
185 $this->assertEqual(count($elements), 0, 'Did not find English variant of tip 1.');