Issue #2028535 by nick_schuch, clemens.tolboom: Provide a TourTestBase class for use by core and contrib modules.

8.0.x
Dries 2013-08-16 15:16:15 -05:00
parent 07298d66e4
commit 52676a7bf6
5 changed files with 204 additions and 25 deletions

View File

@ -8,12 +8,12 @@
namespace Drupal\tour\Tests;
use Drupal\Core\Language\Language;
use Drupal\simpletest\WebTestBase;
use Drupal\tour\Tests\TourTestBase;
/**
* Tests tour functionality.
*/
class TourTest extends WebTestBase {
class TourTest extends TourTestBasic {
/**
* Modules to enable.
@ -22,6 +22,24 @@ class TourTest extends WebTestBase {
*/
public static $modules = array('tour', 'locale', 'language', 'tour_test');
/**
* The permissions required for a logged in user to test tour tips.
*
* @var array
* A list of permissions.
*/
protected $permissions = array('access tour', 'administer languages');
/**
* Tour tip attributes to be tested. Keyed by the path.
*
* @var array
* An array of tip attributes, keyed by path.
*/
protected $tips = array(
'tour-test-1' => array(),
);
public static function getInfo() {
return array(
'name' => 'Tour tests',
@ -30,12 +48,6 @@ class TourTest extends WebTestBase {
);
}
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(array('access tour', 'administer languages')));
}
/**
* Test tour functionality.
*/
@ -49,6 +61,13 @@ class TourTest extends WebTestBase {
));
$this->assertEqual(count($elements), 1, 'Found English variant of tip 1.');
// Test the TourTestBase class assertTourTips() method.
$tips = array();
$tips[] = array('data-id' => 'tour-test-1');
$tips[] = array('data-class' => 'tour-test-5');
$this->assertTourTips($tips);
$this->assertTourTips();
$elements = $this->xpath('//li[@data-id=:data_id and @class=:classes and ./p//a[@href=:href and contains(., :text)]]', array(
':classes' => 'tip-module-tour-test tip-type-text tip-tour-test-1 even last',
':data_id' => 'tour-test-1',

View File

@ -0,0 +1,67 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourTestBase.
*/
namespace Drupal\tour\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests tour functionality.
*/
class TourTestBase extends WebTestBase {
/**
* Assert function to determine if tips rendered to the page
* have a corresponding page element.
*
* @param array $tips
* A list of tips which provide either a "data-id" or "data-class".
*
* @code
* // Basic example.
* $this->assertTourTips();
*
* // Advanced example. The following would be used for multipage or
* // targetting a specific subset of tips.
* $tips = array();
* $tips[] = array('data-id' => 'foo');
* $tips[] = array('data-id' => 'bar');
* $tips[] = array('data-class' => 'baz');
* $this->assertTourTips($tips);
* @endcode
*/
public function assertTourTips($tips = array()) {
// Get the rendered tips and their data-id and data-class attributes.
if (empty($tips)) {
// Tips are rendered as <li> elements inside <ol id="tour">.
$rendered_tips = $this->xpath('//ol[@id = "tour"]//li');
foreach ($rendered_tips as $rendered_tip) {
$attributes = (array) $rendered_tip->attributes();
$tips[] = $attributes['@attributes'];
}
}
// If the tips are still empty we need to fail.
if (empty($tips)) {
$this->fail('Could not find tour tips on the current page.');
}
else {
// Check for corresponding page elements.
foreach ($tips as $tip) {
if (!empty($tip['data-id'])) {
$elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $this->content, TRUE);
$this->assertTrue(!empty($elements) && count($elements) === 1, format_string('Found corresponding page element for tour tip with id #%data-id', array('%data-id' => $tip['data-id'])));
}
else if (!empty($tip['data-class'])) {
$elements = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $this->content, TRUE);
$this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', array('%data-class' => $tip['data-class'])));
}
}
}
}
}

View File

@ -0,0 +1,66 @@
<?php
/**
* @file
* Contains \Drupal\tour\Tests\TourTestBasic.
*/
namespace Drupal\tour\Tests;
use Drupal\tour\Tests\TourTestBase;
/**
* Simple tour tips test base.
*/
abstract class TourTestBasic extends TourTestBase {
/**
* Tour tip attributes to be tested. Keyed by the path.
*
* @var array
* An array of tip attributes, keyed by path.
*
* @code
* protected $tips = array(
* '/foo/bar' => array(
* array('data-id' => 'foo'),
* array('data-class' => 'bar'),
* ),
* );
* @endcode
*/
protected $tips = array();
/**
* An admin user with administrative permissions for tour.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* The permissions required for a logged in user to test tour tips.
*
* @var array
* A list of permissions.
*/
protected $permissions = array('access tour');
protected function setUp() {
parent::setUp();
//Create an admin user to view tour tips.
$this->adminUser = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($this->adminUser);
}
/**
* A simple tip test.
*/
public function testTips() {
foreach ($this->tips as $path => $attributes) {
$this->drupalGet($path);
$this->assertTourTips($attributes);
}
}
}

View File

@ -33,6 +33,13 @@ class TourTestController implements ControllerInterface {
),
'#children' => t('Where does the rain in Spain fail?'),
),
'tip-3' => array(
'#type' => 'container',
'#attributes' => array(
'id' => 'tour-test-3',
),
'#children' => t('Tip created now?'),
),
'tip-4' => array(
'#type' => 'container',
'#attributes' => array(
@ -40,6 +47,20 @@ class TourTestController implements ControllerInterface {
),
'#children' => t('Tip created later?'),
),
'tip-5' => array(
'#type' => 'container',
'#attributes' => array(
'class' => 'tour-test-5',
),
'#children' => t('Tip created later?'),
),
'code-tip-1' => array(
'#type' => 'container',
'#attributes' => array(
'id' => 'tour-code-test-1',
),
'#children' => t('Tip created now?'),
),
);
}

View File

@ -7,27 +7,26 @@
namespace Drupal\views_ui\Tests;
use Drupal\Core\Language\Language;
use Drupal\simpletest\WebTestBase;
use Drupal\tour\Tests\TourTestBase;
/**
* Tests tour functionality.
*/
class ViewsUITourTest extends UITestBase {
class ViewsUITourTest extends TourTestBase {
/**
* An admin user with administrative permissions for views.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('tour');
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_view');
public static $modules = array('views_ui', 'tour');
public static function getInfo() {
return array(
@ -39,16 +38,23 @@ class ViewsUITourTest extends UITestBase {
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(array('access tour', 'administer views')));
$this->adminUser = $this->drupalCreateUser(array('administer views', 'access tour'));
$this->drupalLogin($this->adminUser);
}
/**
* Tests the Views UI tour.
* Tests views_ui tour tip availability.
*/
public function testTourFunctionality() {
$this->drupalGet('admin/structure/views/view/test_view');
$elements = $this->xpath('//ol[@id="tour"]');
$this->assertEqual(count($elements), 1, 'Found a tour on the test view.');
public function testViewsUiTourTips() {
// Create a basic view that shows all content, with a page and a block
// display.
$view['label'] = $this->randomName(16);
$view['id'] = strtolower($this->randomName(16));
$view['page[create]'] = 1;
$view['page[path]'] = $this->randomName(16);
$view_path = $view['page[path]'];
$this->drupalPost('admin/structure/views/add', $view, t('Save and edit'));
$this->assertTourTips();
}
}