annotate core/modules/tour/tests/src/Functional/TourTest.php @ 0:c75dbcec494b

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