Commit 3aa13be9 authored by damiankloip's avatar damiankloip Committed by tim.plunkett

Issue #1637736 by damiankloip, dawehner | Pedro Lozano: Convert user interface test to PSR-0.

parent f155659e
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardBasicTest.
*/
namespace Drupal\views\Tests;
/**
* Tests creating views with the wizard and viewing them on the listing page.
*/
class WizardBasicTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI wizard basic functionality',
'description' => 'Test creating basic views with the wizard and viewing them on the listing page.',
'group' => 'Views UI',
);
}
function testViewsWizardAndListing() {
// Check if we can access the main views admin page.
$this->drupalGet('admin/structure/views');
$this->assertText(t('Add new view'));
// Create a simple and not at all useful view.
$view1 = array();
$view1['human_name'] = $this->randomName(16);
$view1['name'] = strtolower($this->randomName(16));
$view1['description'] = $this->randomName(16);
$view1['page[create]'] = FALSE;
$this->drupalPost('admin/structure/views/add', $view1, t('Save & exit'));
$this->assertText(t('Your view was saved. You may edit it from the list below.'));
$this->assertText($view1['human_name']);
$this->assertText($view1['description']);
foreach(array('delete', 'clone', 'edit') as $operation) {
$this->assertLinkByHref(url('admin/structure/views/view/' . $view1['name'] . '/' . $operation));
}
// This view should not have a block.
$this->drupalGet('admin/structure/block');
$this->assertNoText('View: ' . $view1['human_name']);
// Create two nodes.
$node1 = $this->drupalCreateNode(array('type' => 'page'));
$node2 = $this->drupalCreateNode(array('type' => 'article'));
// Now create a page with simple node listing and an attached feed.
$view2 = array();
$view2['human_name'] = $this->randomName(16);
$view2['name'] = strtolower($this->randomName(16));
$view2['description'] = $this->randomName(16);
$view2['page[create]'] = 1;
$view2['page[title]'] = $this->randomName(16);
$view2['page[path]'] = $this->randomName(16);
$view2['page[feed]'] = 1;
$view2['page[feed_properties][path]'] = $this->randomName(16);
$this->drupalPost('admin/structure/views/add', $view2, t('Save & exit'));
// Since the view has a page, we expect to be automatically redirected to
// it.
$this->assertUrl($view2['page[path]']);
$this->assertText($view2['page[title]']);
$this->assertText($node1->title);
$this->assertText($node2->title);
// Check if we have the feed.
$this->assertLinkByHref(url($view2['page[feed_properties][path]']));
$this->drupalGet($view2['page[feed_properties][path]']);
$this->assertRaw('<rss version="2.0"');
// The feed should have the same title and nodes as the page.
$this->assertText($view2['page[title]']);
$this->assertRaw(url('node/' . $node1->nid, array('absolute' => TRUE)));
$this->assertText($node1->title);
$this->assertRaw(url('node/' . $node2->nid, array('absolute' => TRUE)));
$this->assertText($node2->title);
// Go back to the views page and check if this view is there.
$this->drupalGet('admin/structure/views');
$this->assertText($view2['human_name']);
$this->assertText($view2['description']);
$this->assertLinkByHref(url($view2['page[path]']));
// This view should not have a block.
$this->drupalGet('admin/structure/block');
$this->assertNoText('View: ' . $view2['human_name']);
// Create a view with a page and a block, and filter the listing.
$view3 = array();
$view3['human_name'] = $this->randomName(16);
$view3['name'] = strtolower($this->randomName(16));
$view3['description'] = $this->randomName(16);
$view3['show[wizard_key]'] = 'node';
$view3['show[type]'] = 'page';
$view3['page[create]'] = 1;
$view3['page[title]'] = $this->randomName(16);
$view3['page[path]'] = $this->randomName(16);
$view3['block[create]'] = 1;
$view3['block[title]'] = $this->randomName(16);
$this->drupalPost('admin/structure/views/add', $view3, t('Save & exit'));
// Make sure the view only displays the node we expect.
$this->assertUrl($view3['page[path]']);
$this->assertText($view3['page[title]']);
$this->assertText($node1->title);
$this->assertNoText($node2->title);
// Go back to the views page and check if this view is there.
$this->drupalGet('admin/structure/views');
$this->assertText($view3['human_name']);
$this->assertText($view3['description']);
$this->assertLinkByHref(url($view3['page[path]']));
// Put the block into the first sidebar region.
$this->drupalGet('admin/structure/block');
$this->assertText('View: ' . $view3['human_name']);
$edit = array();
$edit["blocks[views_{$view3['name']}-block][region]"] = 'sidebar_first';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
// Visit a random page (not the one that displays the view itself) and look
// for the expected node title in the block.
$this->drupalGet('user');
$this->assertText($node1->title);
$this->assertNoText($node2->title);
// Check if the export screen works.
$this->drupalGet('admin/structure/views/view/' . $view3['name'] . '/export');
$this->assertRaw('$view = new Drupal\views\View();');
$this->assertRaw($view3['human_name']);
$this->assertRaw($view3['description']);
// Make sure the listing page doesn't show disabled default views.
$this->assertNoText('tracker', t('Default tracker view does not show on the listing page.'));
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardDefaultViewsTest.
*/
namespace Drupal\views\Tests;
/**
* Tests enabling, disabling, and reverting default views via the listing page.
*/
class WizardDefaultViewsTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI default views functionality',
'description' => 'Test enabling, disabling, and reverting default views via the listing page.',
'group' => 'Views UI',
);
}
/**
* Tests default views.
*/
function testDefaultViews() {
// Make sure the front page view starts off as disabled (does not appear on
// the listing page).
$edit_href = 'admin/structure/views/view/frontpage/edit';
$this->drupalGet('admin/structure/views');
// TODO: Disabled default views do now appear on the front page. Test this
// behavior with templates instead.
// $this->assertNoLinkByHref($edit_href);
// Enable the front page view, and make sure it is now visible on the main
// listing page.
$this->drupalGet('admin/structure/views/templates');
$this->clickViewsOperationLink(t('Enable'), '/frontpage/');
$this->assertUrl('admin/structure/views');
$this->assertLinkByHref($edit_href);
// It should not be possible to revert the view yet.
$this->assertNoLink(t('Revert'));
$revert_href = 'admin/structure/views/view/frontpage/revert';
$this->assertNoLinkByHref($revert_href);
// Edit the view and change the title. Make sure that the new title is
// displayed.
$new_title = $this->randomName(16);
$edit = array('title' => $new_title);
$this->drupalPost('admin/structure/views/nojs/display/frontpage/page/title', $edit, t('Apply'));
$this->drupalPost('admin/structure/views/view/frontpage/edit/page', array(), t('Save'));
$this->drupalGet('frontpage');
$this->assertText($new_title);
// It should now be possible to revert the view. Do that, and make sure the
// view title we added above no longer is displayed.
$this->drupalGet('admin/structure/views');
$this->assertLink(t('Revert'));
$this->assertLinkByHref($revert_href);
$this->drupalPost($revert_href, array(), t('Revert'));
$this->drupalGet('frontpage');
$this->assertNoText($new_title);
// Now disable the view, and make sure it stops appearing on the main view
// listing page but instead goes back to displaying on the disabled views
// listing page.
// TODO: Test this behavior with templates instead.
$this->drupalGet('admin/structure/views');
$this->clickViewsOperationLink(t('Disable'), '/frontpage/');
// $this->assertUrl('admin/structure/views');
// $this->assertNoLinkByHref($edit_href);
// The easiest way to verify it appears on the disabled views listing page
// is to try to click the "enable" link from there again.
$this->drupalGet('admin/structure/views/templates');
$this->clickViewsOperationLink(t('Enable'), '/frontpage/');
$this->assertUrl('admin/structure/views');
$this->assertLinkByHref($edit_href);
}
/**
* Click a link to perform an operation on a view.
*
* In general, we expect lots of links titled "enable" or "disable" on the
* various views listing pages, and they might have tokens in them. So we
* need special code to find the correct one to click.
*
* @param $label
* Text between the anchor tags of the desired link.
* @param $unique_href_part
* A unique string that is expected to occur within the href of the desired
* link. For example, if the link URL is expected to look like
* "admin/structure/views/view/frontpage/...", then "/frontpage/" could be
* passed as the expected unique string.
*
* @return
* The page content that results from clicking on the link, or FALSE on
* failure. Failure also results in a failed assertion.
*/
function clickViewsOperationLink($label, $unique_href_part) {
$links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
foreach ($links as $link_index => $link) {
$position = strpos($link['href'], $unique_href_part);
if ($position !== FALSE) {
$index = $link_index;
break;
}
}
$this->assertTrue(isset($index), t('Link to "@label" containing @part found.', array('@label' => $label, '@part' => $unique_href_part)));
if (isset($index)) {
return $this->clickLink($label, $index);
}
else {
return FALSE;
}
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardItemsPerPageTest.
*/
namespace Drupal\views\Tests;
/**
* Tests the ability of the views wizard to specify the number of items per page.
*/
class WizardItemsPerPageTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI wizard items per page functionality',
'description' => 'Test the ability of the views wizard to specify the number of items per page.',
'group' => 'Views UI',
);
}
/**
* Tests the number of items per page.
*/
function testItemsPerPage() {
// Create articles, each with a different creation time so that we can do a
// meaningful sort.
$node1 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME));
$node2 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 1));
$node3 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 2));
$node4 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 3));
$node5 = $this->drupalCreateNode(array('type' => 'article', 'created' => REQUEST_TIME + 4));
// Create a page. This should never appear in the view created below.
$page_node = $this->drupalCreateNode(array('type' => 'page', 'created' => REQUEST_TIME + 2));
// Create a view that sorts newest first, and shows 4 items in the page and
// 3 in the block.
$view = array();
$view['human_name'] = $this->randomName(16);
$view['name'] = strtolower($this->randomName(16));
$view['description'] = $this->randomName(16);
$view['show[wizard_key]'] = 'node';
$view['show[type]'] = 'article';
$view['show[sort]'] = 'created:DESC';
$view['page[create]'] = 1;
$view['page[title]'] = $this->randomName(16);
$view['page[path]'] = $this->randomName(16);
$view['page[items_per_page]'] = 4;
$view['block[create]'] = 1;
$view['block[title]'] = $this->randomName(16);
$view['block[items_per_page]'] = 3;
$this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
// Make sure the page display shows the nodes we expect, and that they
// appear in the expected order.
$this->assertUrl($view['page[path]']);
$this->assertText($view['page[title]']);
$content = $this->drupalGetContent();
$this->assertText($node5->title);
$this->assertText($node4->title);
$this->assertText($node3->title);
$this->assertText($node2->title);
$this->assertNoText($node1->title);
$this->assertNoText($page_node->title);
$pos5 = strpos($content, $node5->title);
$pos4 = strpos($content, $node4->title);
$pos3 = strpos($content, $node3->title);
$pos2 = strpos($content, $node2->title);
$this->assertTrue($pos5 < $pos4 && $pos4 < $pos3 && $pos3 < $pos2, t('The nodes appear in the expected order in the page display.'));
// Put the block into the first sidebar region, visit a page that displays
// the block, and check that the nodes we expect appear in the correct
// order.
$this->drupalGet('admin/structure/block');
$this->assertText('View: ' . $view['human_name']);
$edit = array();
$edit["blocks[views_{$view['name']}-block][region]"] = 'sidebar_first';
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->drupalGet('user');
$content = $this->drupalGetContent();
$this->assertText($node5->title);
$this->assertText($node4->title);
$this->assertText($node3->title);
$this->assertNoText($node2->title);
$this->assertNoText($node1->title);
$this->assertNoText($page_node->title);
$pos5 = strpos($content, $node5->title);
$pos4 = strpos($content, $node4->title);
$pos3 = strpos($content, $node3->title);
$this->assertTrue($pos5 < $pos4 && $pos4 < $pos3, t('The nodes appear in the expected order in the block display.'));
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardJumpMenuTest.
*/
namespace Drupal\views\Tests;
/**
* Tests the ability of the views wizard to create views with a jump menu style plugin.
*/
class WizardJumpMenuTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI wizard jump menu functionality',
'description' => 'Test the ability of the views wizard to create views with a jump menu style plugin.',
'group' => 'Views UI',
);
}
/**
* Tests the jump menu style plugin.
*/
function testJumpMenus() {
// We'll run this test for several different base tables that appear in the
// wizard.
$base_table_methods = array(
'node' => 'createNodeAndGetPath',
'users' => 'createUserAndGetPath',
'comment' => 'createCommentAndGetPath',
'taxonomy_term' => 'createTaxonomyTermAndGetPath',
'file_managed' => 'createFileAndGetPath',
'node_revision' => 'createNodeRevisionAndGetPath',
);
foreach ($base_table_methods as $base_table => $method) {
// For each base table, find the path that we expect the jump menu to
// redirect us to.
$path_info = $this->{$method}();
if (is_array($path_info)) {
$path = $path_info['path'];
$options = isset($path_info['options']) ? $path_info['options'] : array();
}
else {
$path = $path_info;
$options = array();
}
// Create a page view for the specified base table that uses the jump
// menu style plugin.
$view = array();
$view['human_name'] = $this->randomName(16);
$view['name'] = strtolower($this->randomName(16));
$view['description'] = $this->randomName(16);
$view['show[wizard_key]'] = $base_table;
$view['page[create]'] = 1;
$view['page[title]'] = $this->randomName(16);
$view['page[path]'] = $this->randomName(16);
$view['page[style][style_plugin]'] = 'jump_menu';
$view['page[style][row_plugin]'] = 'fields';
$this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
// Submit the jump menu form, and check that we are redirected to the
// expected URL.
$edit = array();
$edit['jump'] = url($path, $options);
// The urls are built with :: to be able to have a unique path all the time,
// so try to find out the real path of $edit.
$view_object = views_get_view($view['name']);
$view_object->preview('page');
$form = $view_object->style_plugin->render();
$jump_options = $form['jump']['#options'];
foreach ($jump_options as $key => $title) {
if (strpos($key, $edit['jump']) !== FALSE) {
$edit['jump'] = $key;
}
}
$this->drupalPost($view['page[path]'], $edit, t('Go'));
$this->assertResponse(200);
$this->assertUrl($path, $options);
}
}
/**
* Helper function to create a node and return its expected path.
*/
function createNodeAndGetPath() {
$node = $this->drupalCreateNode();
return $node->uri();
}
/**
* Helper function to create a user and return its expected path.
*/
function createUserAndGetPath() {
$account = $this->drupalCreateUser();
return $account->uri();
}
/**
* Helper function to create a comment and return its expected path.
*/
function createCommentAndGetPath() {
$node = $this->drupalCreateNode();
$comment = entity_create('comment', array(
'cid' => NULL,
'nid' => $node->nid,
'pid' => 0,
'uid' => 0,
'status' => COMMENT_PUBLISHED,
'subject' => $this->randomName(),
'language' => LANGUAGE_NOT_SPECIFIED,
'comment_body' => array(LANGUAGE_NOT_SPECIFIED => array($this->randomName())),
));
$comment->save();
return $comment->uri();
}
/**
* Helper function to create a taxonomy term and return its expected path.
*/
function createTaxonomyTermAndGetPath() {
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomName(),
'machine_name' => drupal_strtolower($this->randomName()),
));
$vocabulary->save();
$term = entity_create('taxonomy_term', array(
'name' => $this->randomName(),
'vid' => $vocabulary->vid,
'vocabulary_machine_name' => $vocabulary->machine_name,
));
$term->save();
return $term->uri();
}
/**
* Helper function to create a file and return its expected path.
*/
function createFileAndGetPath() {
$file = entity_create('file', array(
'uid' => 1,
'filename' => 'views-ui-jump-menu-test.txt',
'uri' => 'public://views-ui-jump-menu-test.txt',
'filemime' => 'text/plain',
'timestamp' => 1,
'status' => FILE_STATUS_PERMANENT,
));
file_put_contents($file->uri, 'test content');
$file->save();
return file_create_url($file->uri);
}
/**
* Helper function to create a node revision and return its expected path.
*/
function createNodeRevisionAndGetPath() {
// The node needs at least two revisions in order for Drupal to allow
// access to the revision path.
$settings = array('revision' => TRUE);
$node = $this->drupalCreateNode($settings);
$node->vid = NULL;
$node->save();
return 'node/' . $node->nid . '/revisions/' . $node->vid . '/view';
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardMenuTest.
*/
namespace Drupal\views\Tests;
/**
* Tests the ability of the views wizard to put views in a menu.
*/
class WizardMenuTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI wizard menu functionality',
'description' => 'Test the ability of the views wizard to put views in a menu.',
'group' => 'Views UI',
);
}
/**
* Tests the menu functionality.
*/
function testMenus() {
// Create a view with a page display and a menu link in the Main Menu.
$view = array();
$view['human_name'] = $this->randomName(16);
$view['name'] = strtolower($this->randomName(16));
$view['description'] = $this->randomName(16);
$view['page[create]'] = 1;
$view['page[title]'] = $this->randomName(16);
$view['page[path]'] = $this->randomName(16);
$view['page[link]'] = 1;
$view['page[link_properties][menu_name]'] = 'main-menu';
$view['page[link_properties][title]'] = $this->randomName(16);
$this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
// Make sure there is a link to the view from the front page (where we
// expect the main menu to display).
$this->drupalGet('');
$this->assertLink($view['page[link_properties][title]']);
$this->assertLinkByHref(url($view['page[path]']));
// Make sure the link is associated with the main menu.
$links = menu_load_links('main-menu');
$found = FALSE;
foreach ($links as $link) {
if ($link['link_path'] == $view['page[path]']) {
$found = TRUE;
break;
}
}
$this->assertTrue($found, t('Found a link to %path in the main menu', array('%path' => $view['page[path]'])));
}
}
<?php
/**
* @file
* Definition of Drupal\views\Tests\WizardOverrideDisplaysTest.
*/
namespace Drupal\views\Tests;
/**
* Tests that displays can be correctly overridden via the user interface.
*/
class WizardOverrideDisplaysTest extends WizardTestBase {
public static function getInfo() {
return array(
'name' => 'Views UI overridden displays',
'description' => 'Test that displays can be correctly overridden via the user interface.',
'group' => 'Views UI',
);
}
/**
* Tests that displays can be overridden via the UI.
*/
function testOverrideDisplays() {
// Create a basic view that shows all content, with a page and a block
// display.
$view['human_name'] = $this->randomName(16);
$view['name'] = strtolower($this->randomName(16));
$view['page[create]'] = 1;
$view['page[path]'] = $this->randomName(16);
$view['block[create]'] = 1;
$view_path = $view['page[path]'];
$this->drupalPost('admin/structure/views/add', $view, t('Save & exit'));
// Configure its title. Since the page and block both started off with the
// same (empty) title in the views wizard, we expect the wizard to have set