ViewElementTest.php 3.34 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
24
25
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view');

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  /**
   * 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',
        '#name' => 'test_view',
        '#display_id' => 'default',
        '#arguments' => array(25),
      ),
    );
  }

  /**
   * Tests the rendered output and form output of a view element.
   */
  public function testViewElement() {
54
    $view = Views::getView('test_view');
55
    $view->setDisplay();
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    // Set the content as our rendered array.
    $render = $this->render;
    $this->drupalSetContent(drupal_render($render));

    $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');

    $xpath = $this->xpath('//div[@class="views-element-container form-wrapper"]');
    $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.
83
    $view->displayHandlers->get('default')->overrideOption('arguments', array(
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      'age' => array(
        'default_action' => 'ignore',
        'style_plugin' => 'default_summary',
        'style_options' => array(),
        'wildcard' => 'all',
        'wildcard_substitution' => 'All',
        'title' => '',
        'default_argument_type' => 'fixed',
        'default_argument' => '',
        'validate' => array(
          'type' => 'none',
          'fail' => 'not found',
        ),
        'break_phrase' => 0,
        'not' => 0,
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'validate_user_argument_type' => 'uid',
      )
    ));
    $view->save();

    // Test the render array again.
    $render = $this->render;
    $this->drupalSetContent(drupal_render($render));
    // 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);
  }

}