DefaultViewsTest.php 6.46 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\comment\CommentInterface;
11
use Drupal\Core\Language\Language;
12 13 14 15 16 17
use Drupal\simpletest\WebTestBase;
use Drupal\views\ViewExecutable;

/**
 * Tests for views default views.
 */
18
class DefaultViewsTest extends ViewTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('views', 'node', 'search', 'comment', 'taxonomy', 'block');
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  /**
   * 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();

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

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

    // Setup a field and instance.
    $this->field_name = drupal_strtolower($this->randomName());
65
    entity_create('field_entity', array(
66 67
      'name' => $this->field_name,
      'entity_type' => 'node',
68 69 70 71
      'type' => 'taxonomy_term_reference',
      'settings' => array(
        'allowed_values' => array(
          array(
72
            'vocabulary' => $this->vocabulary->id(),
73 74 75 76
            'parent' => '0',
          ),
        ),
      )
77 78
    ))->save();
    entity_create('field_instance', array(
79 80 81
      'field_name' => $this->field_name,
      'entity_type' => 'node',
      'bundle' => 'page',
82
    ))->save();
83 84

    // Create a time in the past for the archive.
85
    $time = REQUEST_TIME - 3600;
86

87 88 89 90
    $this->container->get('comment.manager')->addDefaultField('node', 'page');

    $this->container->get('views.views_data')->clear();

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

      $values = array('created' => $time, 'type' => 'page');
96
      $values[$this->field_name][]['target_id'] = $term->id();
97 98 99 100 101

      // Make every other node promoted.
      if ($i % 2) {
        $values['promote'] = TRUE;
      }
102
      $values['body'][]['value'] = l('Node ' . 1, 'node/' . 1);
103 104 105

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

106
      search_index($node->id(), 'node', $node->body->value, Language::LANGCODE_NOT_SPECIFIED);
107 108

      $comment = array(
109
        'uid' => $user->id(),
110
        'status' => CommentInterface::PUBLISHED,
111 112 113
        'entity_id' => $node->id(),
        'entity_type' => 'node',
        'field_name' => 'comment'
114 115 116
      );
      entity_create('comment', $comment)->save();
    }
117 118 119 120 121

    // Some views, such as the "Who's Online" view, only return results if at
    // least one user is logged in.
    $account = $this->drupalCreateUser(array());
    $this->drupalLogin($account);
122 123 124 125 126 127 128
  }

  /**
   * Test that all Default views work as expected.
   */
  public function testDefaultViews() {
    // Get all default views.
129
    $controller = $this->container->get('entity.manager')->getStorageController('view');
130
    $views = $controller->loadMultiple();
131 132

    foreach ($views as $name => $view_storage) {
133
      $view = $view_storage->getExecutable();
134
      $view->initDisplay();
135
      foreach ($view->storage->get('display') as $display_id => $display) {
136 137 138 139 140 141 142
        $view->setDisplay($display_id);

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

143
        $this->assert(TRUE, format_string('View @view will be executed.', array('@view' => $view->storage->id())));
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        $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) {
160 161
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);
162 163 164 165
    $term = entity_create('taxonomy_term', array(
      'name' => $this->randomName(),
      'description' => $this->randomName(),
      // Use the first available text format.
166
      'format' => $format->format,
167
      'vid' => $vocabulary->id(),
168
      'langcode' => Language::LANGCODE_NOT_SPECIFIED,
169
    ));
170
    $term->save();
171 172 173
    return $term;
  }

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 212 213 214 215 216 217 218 219 220 221 222 223 224
  /**
   * 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'));
    // Create time of additional nodes created in the setup method.
    $created_year_month = date('Ym', REQUEST_TIME - 3600);
    $expected_result = array(
      array(
        'nid' => 1,
        'created_year_month' => $created_year_month,
        '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);
  }

225
}