Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created September 19, 2012 16:06
Show Gist options
  • Save damiankloip/3750510 to your computer and use it in GitHub Desktop.
Save damiankloip/3750510 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Definition of Drupal\views\Tests\DefaultViewsTest.
*/
namespace Drupal\views\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests for views default views.
*/
class DefaultViewsTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('views', 'node', 'search', 'comment', 'taxonomy');
/**
* 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();
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomName(),
'description' => $this->randomName(),
'machine_name' => drupal_strtolower($this->randomName()),
'langcode' => LANGUAGE_NOT_SPECIFIED,
'help' => '',
'nodes' => array('page' => 'page'),
'weight' => mt_rand(0, 10),
));
taxonomy_vocabulary_save($vocabulary);
// Create a time in the past for the archive.
$time = time() - 3600;
for ($i = 0; $i <= 10; $i++) {
$user = $this->drupalCreateUser();
$term = $this->createTerm($vocabulary);
$values = array('created' => $time, 'type' => 'article');
$values['field_tags'][LANGUAGE_NOT_SPECIFIED][]['tid'] = $term->tid;
// Make every other node promoted.
if ($i % 2) {
$values['promote'] = TRUE;
}
$node = $this->drupalCreateNode($values);
$comment = array(
'uid' => $user->uid,
'nid' => $node->nid,
);
entity_create('comment', $comment)->save();
}
}
/**
* Test that all Default views work as expected.
*/
public function testDefaultViews() {
// Get all default views.
$controller = entity_get_controller('view');
$views = $controller->load();
foreach ($views as $name => $view) {
foreach ($view->display as $display_id => $display) {
$view->initDisplay($display_id);
if (array_key_exists($name, $this->viewArgMap)) {
$view->preExecute($this->viewArgMap[$name]);
}
$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) {
$term = entity_create('taxonomy_term', array(
'name' => $this->randomName(),
'description' => $this->randomName(),
// Use the first available text format.
'format' => db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(),
'vid' => $vocabulary->vid,
'langcode' => LANGUAGE_NOT_SPECIFIED,
));
taxonomy_term_save($term);
return $term;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment