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

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

namespace Drupal\views\Tests;

10
use Drupal\Core\Language\Language;
11
12
13
14
15
16
use Drupal\simpletest\WebTestBase;
use Drupal\views\ViewExecutable;

/**
 * Tests for views default views.
 */
17
class DefaultViewsTest extends ViewTestBase {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('views', 'node', 'search', 'comment', 'taxonomy', 'block');

  /**
   * An array of argument arrays to use for default views.
   *
   * @var array
   */
  protected $viewArgMap = array(
    'backlinks' => array(1),
    'taxonomy_term' => array(1),
    'glossary' => array('all'),
  );

  public static function getInfo() {
    return array(
      'name' => 'Default views',
      'description' => 'Tests the default views provided by views',
      'group' => 'Views',
    );
  }

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

48
49
50
    // Create Basic page node type.
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));

51
52
53
    $this->vocabulary = entity_create('taxonomy_vocabulary', array(
      'name' => $this->randomName(),
      'description' => $this->randomName(),
54
      'vid' => drupal_strtolower($this->randomName()),
55
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
56
57
58
59
      'help' => '',
      'nodes' => array('page' => 'page'),
      'weight' => mt_rand(0, 10),
    ));
60
    $this->vocabulary->save();
61
62
63
64
65
66
67
68
69

    // Setup a field and instance.
    $this->field_name = drupal_strtolower($this->randomName());
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
70
            'vocabulary' => $this->vocabulary->id(),
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'node',
      'bundle' => 'page',
    );
    field_create_instance($this->instance);

    // Create a time in the past for the archive.
    $time = time() - 3600;

    for ($i = 0; $i <= 10; $i++) {
      $user = $this->drupalCreateUser();
      $term = $this->createTerm($this->vocabulary);

      $values = array('created' => $time, 'type' => 'page');
92
      $values[$this->field_name][]['tid'] = $term->id();
93
94
95
96
97

      // Make every other node promoted.
      if ($i % 2) {
        $values['promote'] = TRUE;
      }
98
      $values['body'][]['value'] = l('Node ' . 1, 'node/' . 1);
99
100
101

      $node = $this->drupalCreateNode($values);

102
      search_index($node->nid, 'node', $node->body[Language::LANGCODE_NOT_SPECIFIED][0]['value'], Language::LANGCODE_NOT_SPECIFIED);
103
104
105
106

      $comment = array(
        'uid' => $user->uid,
        'nid' => $node->nid,
107
        'node_type' => 'node_type_' . $node->bundle(),
108
109
110
111
112
113
114
115
116
117
      );
      entity_create('comment', $comment)->save();
    }
  }

  /**
   * Test that all Default views work as expected.
   */
  public function testDefaultViews() {
    // Get all default views.
118
    $controller = $this->container->get('plugin.manager.entity')->getStorageController('view');
119
120
121
    $views = $controller->load();

    foreach ($views as $name => $view_storage) {
122
      $view = $view_storage->get('executable');
123
      $view->initDisplay();
124
      foreach ($view->storage->get('display') as $display_id => $display) {
125
126
127
128
129
130
131
        $view->setDisplay($display_id);

        // Add any args if needed.
        if (array_key_exists($name, $this->viewArgMap)) {
          $view->preExecute($this->viewArgMap[$name]);
        }

132
        $this->assert(TRUE, format_string('View @view will be executed.', array('@view' => $view->storage->id())));
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        $view->execute();

        $tokens = array('@name' => $name, '@display_id' => $display_id);
        $this->assertTrue($view->executed, format_string('@name:@display_id has been executed.', $tokens));

        $count = count($view->result);
        $this->assertTrue($count > 0, format_string('@count results returned', array('@count' => $count)));
        $view->destroy();
      }
    }
  }

  /**
   * Returns a new term with random properties in vocabulary $vid.
   */
  function createTerm($vocabulary) {
149
150
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);
151
152
153
154
    $term = entity_create('taxonomy_term', array(
      'name' => $this->randomName(),
      'description' => $this->randomName(),
      // Use the first available text format.
155
      'format' => $format->format,
156
      'vid' => $vocabulary->id(),
157
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
158
    ));
159
    $term->save();
160
161
162
    return $term;
  }

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  /**
   * Tests the archive view.
   */
  public function testArchiveView() {
    // Create additional nodes compared to the one in the setup method.
    // Create two nodes in the same month, and one in each following month.
    $node = array(
      'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT
    );
    $this->drupalCreateNode($node);
    $this->drupalCreateNode($node);
    $node = array(
      'created' => 282891600, // Tue, 19 Dec 1978 05:00:00 GMT
    );
    $this->drupalCreateNode($node);
    $node = array(
      'created' => 285570000, // Fri, 19 Jan 1979 05:00:00 GMT
    );
    $this->drupalCreateNode($node);

    $view = views_get_view('archive');
    $view->setDisplay('page_1');
    $this->executeView($view);
    $column_map = drupal_map_assoc(array('nid', 'created_year_month', 'num_records'));
    $expected_result = array(
      array(
        'nid' => 1,
        'created_year_month' => 201305,
        'num_records' => 11,
      ),
      array(
        'nid' => 15,
        'created_year_month' => 197901,
        'num_records' => 1,
      ),
      array(
        'nid' => 14,
        'created_year_month' => 197812,
        'num_records' => 1,
      ),
      array(
        'nid' => 12,
        'created_year_month' => 197811,
        'num_records' => 2,
      ),
    );
    $this->assertIdenticalResultset($view, $expected_result, $column_map);
  }

212
}