BasicTest.php 3.08 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel;
4

5 6
use Drupal\views\Views;

7
/**
8 9 10
 * A basic query test for Views.
 *
 * @group views
11
 */
12
class BasicTest extends ViewsKernelTestBase {
13

14 15 16 17 18
  /**
   * Views used by this test.
   *
   * @var array
   */
19
  public static $testViews = ['test_view', 'test_simple_argument'];
20

21 22 23 24
  /**
   * Tests a trivial result set.
   */
  public function testSimpleResultSet() {
25
    $view = Views::getView('test_view');
26
    $view->setDisplay();
27 28 29 30 31

    // Execute the view.
    $this->executeView($view);

    // Verify the result.
32
    $this->assertEqual(5, count($view->result), 'The number of returned rows match.');
33
    $this->assertIdenticalResultset($view, $this->dataSet(), [
34 35
      'views_test_data_name' => 'name',
      'views_test_data_age' => 'age',
36
    ]);
37 38 39 40 41 42
  }

  /**
   * Tests filtering of the result set.
   */
  public function testSimpleFiltering() {
43
    $view = Views::getView('test_view');
44
    $view->setDisplay();
45 46

    // Add a filter.
47 48
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'age' => [
49
        'operator' => '<',
50
        'value' => [
51 52 53
          'value' => '28',
          'min' => '',
          'max' => '',
54
        ],
55 56
        'group' => '0',
        'exposed' => FALSE,
57
        'expose' => [
58 59
          'operator' => FALSE,
          'label' => '',
60
        ],
61
        'id' => 'age',
62
        'table' => 'views_test_data',
63 64
        'field' => 'age',
        'relationship' => 'none',
65 66
      ],
    ]);
67 68 69 70 71

    // Execute the view.
    $this->executeView($view);

    // Build the expected result.
72 73
    $dataset = [
      [
74 75 76
        'id' => 1,
        'name' => 'John',
        'age' => 25,
77 78
      ],
      [
79 80 81
        'id' => 2,
        'name' => 'George',
        'age' => 27,
82 83
      ],
      [
84 85 86
        'id' => 4,
        'name' => 'Paul',
        'age' => 26,
87 88
      ],
    ];
89 90

    // Verify the result.
91
    $this->assertEqual(3, count($view->result), 'The number of returned rows match.');
92
    $this->assertIdenticalResultSet($view, $dataset, [
93 94
      'views_test_data_name' => 'name',
      'views_test_data_age' => 'age',
95
    ]);
96 97 98 99 100 101 102
  }

  /**
   * Tests simple argument.
   */
  public function testSimpleArgument() {
    // Execute with a view
103
    $view = Views::getView('test_simple_argument');
104
    $view->setArguments([27]);
105 106 107
    $this->executeView($view);

    // Build the expected result.
108 109
    $dataset = [
      [
110 111 112
        'id' => 2,
        'name' => 'George',
        'age' => 27,
113 114
      ],
    ];
115 116

    // Verify the result.
117
    $this->assertEqual(1, count($view->result), 'The number of returned rows match.');
118
    $this->assertIdenticalResultSet($view, $dataset, [
119 120
      'views_test_data_name' => 'name',
      'views_test_data_age' => 'age',
121
    ]);
122 123

    // Test "show all" if no argument is present.
124
    $view = Views::getView('test_simple_argument');
125 126 127 128 129
    $this->executeView($view);

    // Build the expected result.
    $dataset = $this->dataSet();

130
    $this->assertEqual(5, count($view->result), 'The number of returned rows match.');
131
    $this->assertIdenticalResultSet($view, $dataset, [
132 133
      'views_test_data_name' => 'name',
      'views_test_data_age' => 'age',
134
    ]);
135
  }
136

137
}