DefaultViewsTest.php 4.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
<?php

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

namespace Drupal\views\Tests;

use Drupal\simpletest\WebTestBase;
use Drupal\views\ViewExecutable;

/**
 * Tests for views default views.
 */
class DefaultViewsTest extends WebTestBase {

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

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

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

    // 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(
69
            'vocabulary' => $this->vocabulary->id(),
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'node',
      'bundle' => 'page',
      'widget' => array(
        'type' => 'options_select',
      ),
    );
    field_create_instance($this->instance);
85 86 87 88 89
    entity_get_display('node', 'page', 'full')
      ->setComponent($this->field_name, array(
        'type' => 'taxonomy_term_reference_link',
      ))
      ->save();
90 91 92 93 94 95 96 97 98

    // 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');
99
      $values[$this->field_name][]['tid'] = $term->id();
100 101 102 103 104

      // Make every other node promoted.
      if ($i % 2) {
        $values['promote'] = TRUE;
      }
105
      $values['body'][]['value'] = l('Node ' . 1, 'node/' . 1);
106 107 108 109 110 111 112 113

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

      search_index($node->nid, 'node', $node->body[LANGUAGE_NOT_SPECIFIED][0]['value'], LANGUAGE_NOT_SPECIFIED);

      $comment = array(
        'uid' => $user->uid,
        'nid' => $node->nid,
114
        'node_type' => 'node_type_' . $node->bundle(),
115 116 117 118 119 120 121 122 123 124
      );
      entity_create('comment', $comment)->save();
    }
  }

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

    foreach ($views as $name => $view_storage) {
129
      $view = $view_storage->get('executable');
130
      $view->initDisplay();
131
      foreach ($view->storage->get('display') as $display_id => $display) {
132 133 134 135 136 137 138
        $view->setDisplay($display_id);

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

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

}