Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Apress.Pro.Drupal.7.Development.3rd.Edition.Dec.2010.pdf
Скачиваний:
73
Добавлен:
14.03.2016
Размер:
12.64 Mб
Скачать

Download from Wow! eBook <www.wowebook.com>

CHAPTER 25 TESTING

The testing framework also provides the ability to display the values associated with variables and entities like a node object, providing further assistance in debugging why a test failed. If proof is in the pudding, then the Drupal testing framework is double chocolate pudding—rich and delicious.

How Tests Are Defined

Tests are typically associated with a module and as such are defined at the module level. In the foregoing case of the Blog test, the developers of the Blog module created a new file in the Blog module directory named blog.test. The content of the blog.test module shows how the developer set up the environment and the individual test conditions that will be executed when the test suite is run.

The first thing to notice is that testing is very object-oriented. A test is essentially an object that is created from a class that is based on the DrupalWebTestCase or DrupalUnitTestCase classes. By taking an object-oriented approach, our test class inherits all of the functionality defined in the base class, allowing you the developer to focus on what tests you want to run rather than coding scripts to handle things like loading a page, figuring out how to enter text on a form, etc.

The next step is to define any instance variables that will be used throughout the testing process. In the case of the Blog tests, the developer defined three variables, each representing a specific user with specific assigned privileges.

<?php

class BlogTestCase extends DrupalWebTestCase { protected $big_user;

protected $own_user; protected $any_user;

The next step is to define the name, description, and the group associated with this set of tests, using the getInfo function. This is the information that appears on the Configuration -> Testing page. The group field places this test in the Blog group. You can see the name and description of the test if you click the arrow to the left of Blog on the Tests page. This approach allows you to define logical sets of tests and associate those tests with a specific module.

public static function getInfo() { return array(

'name' => 'Blog functionality',

'description' => 'Create, view, edit, delete, and change blog entries and verify its consistency in the database.',

'group' => 'Blog',

);

}

The setup process is the next section defined by the developer. When the testing process begins, the first thing that the test framework does is to create a new base Drupal instance. If our tests require modules, user accounts, content types, files in the files directory, or anything else beyond a base Drupal 7 core install, the setUp() function is where you perform those setup tasks. In the case of the Blog tests, the only thing we need to do is to first enable the Blog module, which is performed through the parent:setUp(‘blog’) function call. If there were additional modules that needed to be enabled, you would add each module as an additional parameter such as parent::setUp('blog', 'ctools', 'panels',

'date').

550

CHAPTER 25 TESTING

The next step creates the three user accounts we want to use, assigning specific permissions to each of the three accounts. We create a new user by executing the drupalCreateUser method that is associated with our current testing object—referenced as $this. To assign permissions, we simply pass an array of permissions that we want assigned to each user account.

/**

* Enable modules and create users with specific permissions. */

function setUp() { parent::setUp('blog'); // Create users.

$this->big_user = $this->drupalCreateUser(array('administer blocks')); $this->own_user = $this->drupalCreateUser(array('create blog content', 'edit own blog

content', 'delete own blog content'));

$this->any_user = $this->drupalCreateUser(array('create blog content', 'edit any blog content', 'delete any blog content', 'access administration pages'));

}

The next function acts as the primary controller for the execution of the tests.

Note Any function that starts with a test will be automatically discovered and executed.

In this function, the test script logs the admin user, big_user, into the site, followed by enabling the recent blog posting block and assigning it to the sidebar_second region. The next step configures the recent blog posts block, setting the number of posts that will appear in the block to five. The next step executes the doBasicTests function (defined later) using the any_user and own_user accounts, followed by execution of other test functions defined elsewhere in this test script.

/**

* Login users, create blog nodes, and test blog functionality through the admin and user interfaces.

*/

function testBlog() {

// Login the admin user. $this->drupalLogin($this->big_user);

//Enable the recent blog block. $edit = array();

$edit['blog_recent[region]'] = 'sidebar_second'; $this->drupalPost('admin/structure/block', $edit, t('Save blocks')); $this->assertResponse(200);

//Verify ability to change number of recent blog posts in block.

$edit = array(); $edit['blog_block_count'] = 5;

$this->drupalPost('admin/structure/block/manage/blog/recent/configure', $edit, t('Save block'));

551

CHAPTER 25 TESTING

$this->assertEqual(variable_get('blog_block_count', 10), 5, t('Number of recent blog posts changed.'));

// Do basic tests for each user. $this->doBasicTests($this->any_user, TRUE); $this->doBasicTests($this->own_user, FALSE);

// Create another blog node for the any blog user.

$node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->any_user->uid));

//Verify the own blog user only has access to the blog view node. $this->verifyBlogs($this->any_user, $node, FALSE, 403);

//Create another blog node for the own blog user.

$node = $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->own_user->uid)); // Login the any blog user.

$this->drupalLogin($this->any_user);

// Verify the any blog user has access to all the blog nodes. $this->verifyBlogs($this->own_user, $node, TRUE);

}

The next set of functions executes specific functional tests. The testUnprivilegedUser function checks to make sure that someone who shouldn’t have the ability to post a blog on the web site. The process for testing this functionality is first attempting to create a node using the drupalCreateNode method, which takes an array of parameters that are used to attempt to create a node. The next step is to log onto the site by using the drupalLogin method and the big_user that was created in the setup function. After the user is logged in, the next step is to navigate to the big_user’s blog page using the drupalGet functionality. The next step checks the HTTP response code returned from attempting to navigate to that user’s blog page. To test whether the user successfully landed on his or her own blog page, the developer used the assertResponse method to check the HTTP response code. If the code is 200 (success), then the test was successful and logged as such. The developer continues the test by checking to see if the page title is set to the user’s name and “blog” using the assertTitle method; if it is, then the success is logged. The final test is to see whether the text “You are not allowed to post a new blog entry” is displayed somewhere on the page. The developer uses the assertText function, which scans the page looking for an exact match of the string that is passed as the first parameter. If there is a match, then the test succeeded and the result is logged using the second parameter passed in the assertText method.

/**

*Confirm that the "You are not allowed to post a new blog entry." message

*shows up if a user submitted blog entries, has been denied that

*permission, and goes to the blog page.

*/

function testUnprivilegedUser() {

// Create a blog node for a user with no blog permissions. $this->drupalCreateNode(array('type' => 'blog', 'uid' => $this->big_user->uid));

$this->drupalLogin($this->big_user);

$this->drupalGet('blog/' . $this->big_user->uid); $this->assertResponse(200);

552

CHAPTER 25 TESTING

$this->assertTitle(t("@name's blog", array('@name' => format_username($this->big_user)))

. ' | Drupal', t('Blog title was displayed'));

$this->assertText(t('You are not allowed to post a new blog entry.'), t('No new entries can be posted without the right permission'));

}

The next test checks to see whether a user can view another user’s blog page, when that other user doesn’t have any blog postings. The process is similar to the foregoing test. The developer logs onto the site using the drupalLogin method, passing the big_user as the user to log in as. The next step is to navigate to the own_user’s blog page using the drupalGet method. The developer checks to see whether the page loaded successfully using the assertResponse method, followed by a check to make sure the page title is properly displayed, using the assertTitle method. The text is properly displayed for a user’s blog page when that user doesn’t have any blog entries (using the assertText method).

/**

* View the blog of a user with no blog entries as another user. */

function testBlogPageNoEntries() { $this->drupalLogin($this->big_user);

$this->drupalGet('blog/' . $this->own_user->uid); $this->assertResponse(200);

$this->assertTitle(t("@name's blog", array('@name' => format_username($this->own_user)))

. ' | Drupal', t('Blog title was displayed'));

$this->assertText(t('@author has not created any blog entries.', array('@author' => format_username($this->own_user))), t('Users blog displayed with no entries'));

}

The doBasicTests function does just what its name implies, executes other test functions defined elsewhere in this test script. The function logs the user in and executes the tests that verify blogs, creates nodes, and verifies blog links.

/**

*Run basic tests on the indicated user.

*@param object $user

*The logged in user.

*@param boolean $admin

*User has 'access administration pages' privilege.

*/

private function doBasicTests($user, $admin) { // Login the user. $this->drupalLogin($user);

// Create blog node.

$node = $this->drupalCreateNode(array('type' => 'blog')); // Verify the user has access to all the blog nodes. $this->verifyBlogs($user, $node, $admin);

553

CHAPTER 25 TESTING

//Create one more node to test the blog page with more than one node $this->drupalCreateNode(array('type' => 'blog', 'uid' => $user->uid));

//Verify the blog links are displayed.

$this->verifyBlogLinks($user);

}

The following functions continue to use the patterns just defined to perform various tests and verify and record results.

/**

*Verify the logged in user has the desired access to the various blog nodes.

*@param object $node_user

*The user who creates the node.

*@param object $node

*A node object.

*@param boolean $admin

*User has 'access administration pages' privilege.

*@param integer $response

*HTTP response code.

*/

private function verifyBlogs($node_user, $node, $admin, $response = 200) { $response2 = ($admin) ? 200 : 403;

// View blog help node. $this->drupalGet('admin/help/blog'); $this->assertResponse($response2); if ($response2 == 200) {

$this->assertTitle(t('Blog | Drupal'), t('Blog help node was displayed')); $this->assertText(t('Blog'), t('Blog help node was displayed'));

}

//Verify the blog block was displayed. $this->drupalGet(''); $this->assertResponse(200);

$this->assertText(t('Recent blog posts'), t('Blog block was displayed'));

//View blog node.

$this->drupalGet('node/' . $node->nid); $this->assertResponse(200);

$this->assertTitle($node->title . ' | Drupal', t('Blog node was displayed')); $breadcrumb = array(

l(t('Home'), NULL), l(t('Blogs'), 'blog'),

l(t("!name's blog", array('!name' => format_username($node_user))), 'blog/' . $node_user->uid),

);

$this->assertRaw(theme('breadcrumb', array('breadcrumb' => $breadcrumb)), t('Breadcrumbs were displayed'));

554

CHAPTER 25 TESTING

// View blog edit node.

$this->drupalGet('node/' . $node->nid . '/edit'); $this->assertResponse($response);

if ($response == 200) {

$this->assertTitle('Edit Blog entry ' . $node->title . ' | Drupal', t('Blog edit node was displayed'));

}

if ($response == 200) { // Edit blog node. $edit = array();

$langcode = LANGUAGE_NONE; $edit["title"] = 'node/' . $node->nid;

$edit["body[$langcode][0][value]"] = $this->randomName(256); $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->assertRaw(t('Blog entry %title has been updated.', array('%title' =>

$edit["title"])), t('Blog node was edited'));

// Delete blog node.

$this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete')); $this->assertResponse($response);

$this->assertRaw(t('Blog entry %title has been deleted.', array('%title' => $edit["title"])), t('Blog node was deleted'));

}

}

/**

*Verify the blog links are displayed to the logged in user.

*@param object $user

*The logged in user.

*/

private function verifyBlogLinks($user) {

// Confirm blog entries link exists on the user page. $this->drupalGet('user/' . $user->uid); $this->assertResponse(200);

$this->assertText(t('View recent blog entries'), t('View recent blog entries link was displayed'));

// Confirm the recent blog entries link goes to the user's blog page. $this->clickLink('View recent blog entries');

$this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), t('View recent blog entries link target was correct'));

// Confirm a blog page was displayed. $this->drupalGet('blog'); $this->assertResponse(200);

$this->assertTitle('Blogs | Drupal', t('Blog page was displayed')); $this->assertText(t('Home'), t('Breadcrumbs were displayed')); $this->assertLink(t('Create new blog entry'));

555

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]