TourTest.php 7.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

/**
 * @file
 * Contains \Drupal\tour\Tests\TourTest.
 */

namespace Drupal\tour\Tests;

use Drupal\Core\Language\Language;

/**
 * Tests tour functionality.
 */
15
class TourTest extends TourTestBasic {
16
17
18
19
20
21
22
23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('tour', 'locale', 'language', 'tour_test');

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  /**
   * The permissions required for a logged in user to test tour tips.
   *
   * @var array
   *   A list of permissions.
   */
  protected $permissions = array('access tour', 'administer languages');

  /**
   * Tour tip attributes to be tested. Keyed by the path.
   *
   * @var array
   *   An array of tip attributes, keyed by path.
   */
  protected $tips = array(
    'tour-test-1' => array(),
  );

42
43
44
45
46
47
48
49
50
51
52
53
  public static function getInfo() {
    return array(
      'name' => 'Tour tests',
      'description' => 'Test the functionality of tour tips.',
      'group' => 'Tour',
    );
  }

  /**
   * Test tour functionality.
   */
  public function testTourFunctionality() {
54
    // Navigate to tour-test-1 and verify the tour_test_1 tip is found with appropriate classes.
55
    $this->drupalGet('tour-test-1');
56
    $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', array(
57
      ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
58
      ':data_id' => 'tour-test-1',
59
      ':text' => 'The first tip',
60
61
62
    ));
    $this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');

63
64
65
66
67
68
69
    // Test the TourTestBase class assertTourTips() method.
    $tips = array();
    $tips[] = array('data-id' => 'tour-test-1');
    $tips[] = array('data-class' => 'tour-test-5');
    $this->assertTourTips($tips);
    $this->assertTourTips();

70
    $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./p//a[@href=:href and contains(., :text)]]', array(
71
      ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
72
      ':data_id' => 'tour-test-1',
73
74
      ':href' =>  url('<front>', array('absolute' => TRUE)),
      ':text' => 'Drupal',
75
76
77
    ));
    $this->assertEqual(count($elements), 1, 'Found Token replacement.');

78
79
    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-2',
80
      ':text' => 'The quick brown fox',
81
82
83
84
85
    ));
    $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 2.');

    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-1',
86
      ':text' => 'La pioggia cade in spagna',
87
88
89
90
91
92
93
94
95
96
    ));
    $this->assertNotEqual(count($elements), 1, 'Did not find Italian variant of tip 1.');

    // Ensure that plugin's work.
    $this->assertRaw('img src="http://local/image.png"', 'Image plugin tip found.');

    // Navigate to tour-test-2/subpath and verify the tour_test_2 tip is found.
    $this->drupalGet('tour-test-2/subpath');
    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-2',
97
      ':text' => 'The quick brown fox',
98
99
100
101
102
    ));
    $this->assertEqual(count($elements), 1, 'Found English variant of tip 2.');

    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-1',
103
      ':text' => 'The first tip',
104
105
106
107
108
    ));
    $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');

    // Enable Italian language and navigate to it/tour-test1 and verify italian
    // version of tip is found.
109
    language_save(new Language(array('id' => 'it')));
110
111
112
113
    $this->drupalGet('it/tour-test-1');

    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-1',
114
      ':text' => 'La pioggia cade in spagna',
115
116
117
118
119
    ));
    $this->assertEqual(count($elements), 1, 'Found Italian variant of tip 1.');

    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-1',
120
      ':text' => 'The first tip',
121
122
123
    ));
    $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');

124
    language_save(new Language(array('id' => 'en')));
125
126
127
128
129
130

    // Programmatically create a tour for use through the remainder of the test.
    entity_create('tour', array(
      'id' => 'tour-entity-create-test-en',
      'label' => 'Tour test english',
      'langcode' => 'en',
131
132
      'routes' => array(
        array('route_name' => 'tour_test.1'),
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      ),
      'tips' => array(
        'tour-test-1' => array(
          'id' => 'tour-code-test-1',
          'plugin' => 'text',
          'label' => 'The rain in spain',
          'body' => 'Falls mostly on the plain.',
          'weight' => '100',
          'attributes' => array(
            'data-id' => 'tour-code-test-1',
          ),
        ),
      ),
    ))->save();

    $this->drupalGet('tour-test-1');

    // Load it back from the database and verify storage worked.
    $entity_save_tip = entity_load('tour', 'tour-entity-create-test-en');
    // Verify that hook_ENTITY_TYPE_load() integration worked.
    $this->assertEqual($entity_save_tip->loaded, 'Load hooks work');
    // Verify that hook_ENTITY_TYPE_presave() integration worked.
    $this->assertEqual($entity_save_tip->label(), 'Tour test english alter');

    // Navigate to tour-test-1 and verify the new tip is found.
    $this->drupalGet('tour-test-1');
    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-code-test-1',
161
      ':text' => 'The rain in spain',
162
163
164
165
    ));
    $this->assertEqual(count($elements), 1, 'Found the required tip markup for tip 4');

    // Verify that the weight sorting works by ensuring the lower weight item
166
167
    // (tip 4) has the 'End tour' button.
    $elements = $this->xpath('//li[@data-id=:data_id and @data-text=:text]', array(
168
      ':data_id' => 'tour-code-test-1',
169
      ':text' => 'End tour',
170
    ));
171
    $this->assertEqual(count($elements), 1, 'Found code tip was weighted last and had "End tour".');
172
173
174

    // Test hook_tour_alter().
    $this->assertText('Altered by hook_tour_tips_alter');
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

    // Navigate to tour-test-3 and verify the tour_test_1 tip is found with
    // appropriate classes.
    $this->drupalGet('tour-test-3/foo');
    $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', array(
      ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
      ':data_id' => 'tour-test-1',
      ':text' => 'The first tip',
    ));
    $this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');

    // Navigate to tour-test-3 and verify the tour_test_1 tip is not found with
    // appropriate classes.
    $this->drupalGet('tour-test-3/bar');
    $elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./h2[contains(., :text)]]', array(
      ':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1',
      ':data_id' => 'tour-test-1',
      ':text' => 'The first tip',
    ));
    $this->assertEqual(count($elements), 0, 'Found English variant of tip 1.');
195
196
  }
}