TourTest.php 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

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

namespace Drupal\tour\Tests;

use Drupal\Core\Language\Language;
11
use Drupal\tour\Tests\TourTestBase;
12 13 14 15

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

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

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * 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(),
  );

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

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

64 65 66 67 68 69 70
    // 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();

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

79 80
    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-2',
81
      ':text' => 'The quick brown fox',
82 83 84 85 86
    ));
    $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',
87
      ':text' => 'La pioggia cade in spagna',
88 89 90 91 92 93 94 95 96 97
    ));
    $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',
98
      ':text' => 'The quick brown fox',
99 100 101 102 103
    ));
    $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',
104
      ':text' => 'The first tip',
105 106 107 108 109
    ));
    $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.
110
    language_save(new Language(array('id' => 'it')));
111 112 113 114
    $this->drupalGet('it/tour-test-1');

    $elements = $this->xpath('//li[@data-id=:data_id and ./h2[contains(., :text)]]', array(
      ':data_id' => 'tour-test-1',
115
      ':text' => 'La pioggia cade in spagna',
116 117 118 119 120
    ));
    $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',
121
      ':text' => 'The first tip',
122 123 124
    ));
    $this->assertNotEqual(count($elements), 1, 'Did not find English variant of tip 1.');

125
    language_save(new Language(array('id' => 'en')));
126 127 128 129 130 131 132 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 161

    // 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',
      'paths' => array(
        'tour-test-1',
      ),
      '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',
162
      ':text' => 'The rain in spain',
163 164 165 166
    ));
    $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
167 168
    // (tip 4) has the 'End tour' button.
    $elements = $this->xpath('//li[@data-id=:data_id and @data-text=:text]', array(
169
      ':data_id' => 'tour-code-test-1',
170
      ':text' => 'End tour',
171
    ));
172
    $this->assertEqual(count($elements), 1, 'Found code tip was weighted last and had "End tour".');
173 174 175 176 177

    // Test hook_tour_alter().
    $this->assertText('Altered by hook_tour_tips_alter');
  }
}