ViewElementTest.php 5.66 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\views\Tests\ViewElementTest.
 */

namespace Drupal\views\Tests;

10 11
use Drupal\views\Views;

12
/**
13 14 15
 * Tests the view render element.
 *
 * @group views
16 17 18
 */
class ViewElementTest extends ViewTestBase {

19 20 21 22 23
  /**
   * Views used by this test.
   *
   * @var array
   */
24
  public static $testViews = array('test_view_embed');
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * The raw render data array to use in tests.
   *
   * @var array
   */
  protected $render;

  protected function setUp() {
    parent::setUp();

    $this->enableViewsTestModule();

    // Set up a render array to use. We need to copy this as drupal_render
    // passes by reference.
    $this->render = array(
      'view' => array(
        '#type' => 'view',
43
        '#name' => 'test_view_embed',
44 45
        '#display_id' => 'default',
        '#arguments' => array(25),
46
        '#embed' => FALSE,
47 48 49 50 51 52 53 54
      ),
    );
  }

  /**
   * Tests the rendered output and form output of a view element.
   */
  public function testViewElement() {
55
    $view = Views::getView('test_view_embed');
56
    $view->setDisplay();
57 58 59

    // Set the content as our rendered array.
    $render = $this->render;
60
    $this->setRawContent(drupal_render($render));
61 62 63 64 65 66 67 68 69 70 71 72 73

    $xpath = $this->xpath('//div[@class="views-element-container"]');
    $this->assertTrue($xpath, 'The view container has been found in the rendered output.');

    $xpath = $this->xpath('//div[@class="view-content"]');
    $this->assertTrue($xpath, 'The view content has been found in the rendered output.');
    // There should be 5 rows in the results.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 5);

    // Test a form.
    $this->drupalGet('views_test_data_element_form');

74
    $xpath = $this->xpath('//div[@class="views-element-container js-form-wrapper form-wrapper"]');
75 76 77 78 79 80 81 82 83
    $this->assertTrue($xpath, 'The view container has been found on the form.');

    $xpath = $this->xpath('//div[@class="view-content"]');
    $this->assertTrue($xpath, 'The view content has been found on the form.');
    // There should be 5 rows in the results.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 5);

    // Add an argument and save the view.
84
    $view->displayHandlers->get('default')->overrideOption('arguments', array(
85 86 87 88 89 90 91 92
      'age' => array(
        'default_action' => 'ignore',
        'title' => '',
        'default_argument_type' => 'fixed',
        'validate' => array(
          'type' => 'none',
          'fail' => 'not found',
        ),
93 94
        'break_phrase' => FALSE,
        'not' => FALSE,
95 96 97
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
98
        'plugin_id' => 'numeric',
99 100 101 102 103 104
      )
    ));
    $view->save();

    // Test the render array again.
    $render = $this->render;
105
    $this->setRawContent(drupal_render($render));
106 107 108 109 110 111 112 113 114 115
    // There should be 1 row in the results, 'John' arg 25.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 1);

    // Test that the form has the same expected result.
    $this->drupalGet('views_test_data_element_form');
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 1);
  }

116 117 118 119 120 121 122 123 124 125 126
  /**
   * Tests the rendered output and form output of a view element, using the
   * embed display plugin.
   */
  public function testViewElementEmbed() {
    $view = Views::getView('test_view_embed');
    $view->setDisplay('embed_1');

    // Set the content as our rendered array.
    $render = $this->render;
    $render['#embed'] = TRUE;
127
    $this->setRawContent(drupal_render($render));
128 129 130 131 132 133 134 135 136 137 138 139 140

    $xpath = $this->xpath('//div[@class="views-element-container"]');
    $this->assertTrue($xpath, 'The view container has been found in the rendered output.');

    $xpath = $this->xpath('//div[@class="view-content"]');
    $this->assertTrue($xpath, 'The view content has been found in the rendered output.');
    // There should be 5 rows in the results.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 5);

    // Test a form.
    $this->drupalGet('views_test_data_element_embed_form');

141
    $xpath = $this->xpath('//div[@class="views-element-container js-form-wrapper form-wrapper"]');
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    $this->assertTrue($xpath, 'The view container has been found on the form.');

    $xpath = $this->xpath('//div[@class="view-content"]');
    $this->assertTrue($xpath, 'The view content has been found on the form.');
    // There should be 5 rows in the results.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 5);

    // Add an argument and save the view.
    $view->displayHandlers->get('default')->overrideOption('arguments', array(
      'age' => array(
        'default_action' => 'ignore',
        'title' => '',
        'default_argument_type' => 'fixed',
        'validate' => array(
          'type' => 'none',
          'fail' => 'not found',
        ),
        'break_phrase' => FALSE,
        'not' => FALSE,
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'plugin_id' => 'numeric',
      )
    ));
    $view->save();

    // Test the render array again.
    $render = $this->render;
    $render['#embed'] = TRUE;
173
    $this->setRawContent(drupal_render($render));
174 175 176 177 178 179 180 181 182 183
    // There should be 1 row in the results, 'John' arg 25.
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 1);

    // Test that the form has the same expected result.
    $this->drupalGet('views_test_data_element_embed_form');
    $xpath = $this->xpath('//div[@class="view-content"]/div');
    $this->assertEqual(count($xpath), 1);
  }

184
}