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

CHAPTER 25 TESTING

// Confirm a blog page was displayed per user. $this->drupalGet('blog/' . $user->uid);

$this->assertTitle(t("@name's blog | Drupal", array('@name' => format_username($user))), t('User blog node was displayed'));

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

$this->assertTitle(t('Drupal blogs'), t('Blog feed was displayed'));

//Confirm a blog feed was displayed per user.

$this->drupalGet('blog/' . $user->uid . '/feed');

$this->assertTitle(t("@name's blog", array('@name' => format_username($user))), t('User blog feed was displayed'));

}

}

Test Functions

The blog testing script exercised several of the test functions that are included in testing framework. As you begin to document the functionality that you want to test on your site, you will need to identify “how” you are going to execute those tests. The “how” will be by stringing together one or more functions that emulate an end user performing functions on your site. Assertions validate that the tests that you execute through these functions deliver the results that you expect. The functions that you can use are defined in Table 25-1, and the assertions that you use to verify the results are listed in Table 25-2.

Table 25-1. Testing Functions

Function

Description

 

 

$this->drupalGet($path, $options=array())

This function executes a get request to a URL on the site. $path indicates the page that will be visited, and $options contains additional data that may be passed to URL in order to determine the URL to visit. The content will be loaded and saved into $this->_content where it can be retrieved using the $this->drupalGetContent() function.

$this->drupalPost($path, $edit, $submit,

This function executes a post request on a Drupal

$reporting=TRUE)

page. $path indicates a page containing a form that

 

will be filled with data described in the $edit

 

parameter. Then the button indicated by $submit

 

will be clicked. The $edit data should be an array

 

where each index is the value of the “name”

 

attribute of the HTML form element.

556

CHAPTER 25 TESTING

Function

Description

 

 

$this->clickLink($label, $index=0)

This function follows a link on the current page. The $label parameter should contain the text associated with the link. If there are multiple links on the page with the same text, use the $index parameter to indicate which link to click by counting the number of links on the page with the same text by viewing the page source and counting the links starting at the top.

$this->drupalCreateUser($permissions = NULL)

This function creates a new user and assigns the

 

permissions listed in the $permissions parameter

 

(e.g., array(‘access comments’, ‘access content’,

 

‘post comments’). The function returns a fully

 

populated Drupal user object with an additional

 

value named pass_raw that contains the non-

 

hashed password. The function also creates a user

 

role with the specified permissions and assigns

 

that role to the user account.

$this->drupalLogin($user = NULL)

This function logs a user into your site using the

 

virtual browser created during the startup process.

 

The $user parameter is a standard Drupal user

 

object. If you fail to pass a user object to the

 

function, the function will create a new user for

 

you.

$this->drupalLogout()

This function logs out the current user in the

 

virtual browser. This function is automatically

 

called by the $this->drupalLogin function,

 

ensuring that only one user is logged in at a time.

 

This is a key point to consider when writing your

 

scripts—only one user can be logged in at any

 

given moment.

$this->drupalCreateRole($permissions = NULL)

This function creates a Drupal role using the

 

permissions passed in the $permissions

 

parameter. This function returns a value that is the

 

role-id or FALSE on failure. In most cases, this

 

function doesn’t need to be called as the $this-

 

>drupalCreateUser function automatically creates

 

a new role based on the permissions defined in its

 

$permissions parameter.

$this->randomString($number = 8)

This function returns a string of a length defined by the $number parameter, where each character is between ASCII codes 32 to 126. You can use this function to create node titles, node bodies, etc.

557

CHAPTER 25 TESTING

Continued

Function

Description

 

 

$this->randomName($number = 8)

This function returns a string of a length defined by the $number parameter, where each character is between ASCII codes 32 to 126. You can use this function to create node titles, node bodies, etc.

$this->drupalCreateContentType($settings)

Thi

functions creates a new custom content type

 

based on the settings defined in the $settings

 

parameter. The default values for nodes are

 

automatically set for you. You can override those

 

settings through the $settings parameter. An

 

example of $settings could be:

 

$settings = array(

 

 

‘type’ => ‘event’,

 

 

‘title’ => ‘Event Title’,

 

 

‘body_label’ => ‘Event Description’,

 

);

 

$this->drupalCreateNode($settings)

This

nction creates afunew node using default

values. You can override those settings or append new data to the settings through the $settings parameter. An example of $settings could be:

$settings = array( ‘type’ => ‘event’,

‘event_date’ => ‘2012-12-21 00:00:00’,

);

where the content type is set to event instead of the default content type for your site, and a new field named event_date is created and assigned a value. The default values associated with creating a node are:

558

CHAPTER 25 TESTING

Function

Description

 

 

$this->cronRun()

$this->drupalGetNodeByTitle($title)

$this->drupalGetTestFiles($type, $size = NULL)

$defaults = array(

 

'body'

=>

$this->randomName(32),

'title'

=>

$this->randomName(8),

'comment'

=>

2,

'changed'

=>

time(),

'format'

=>

FILTER_FORMAT_DEFAULT,

'moderate'

=>

0,

'promote'

=>

0,

'revision'

=>

1,

'log'

=>

'',

'status'

=>

1,

'sticky'

=>

0,

'type'

=>

'page',

'revisions' =>

NULL,

'taxonomy'

=>

NULL,

 

);

This function executes cron.

This

on retrievesfunctia node by the title defined

in the $title parameter.

This function returns a list of files that match the types defined in the $type parameter (e.g., binary, html, image, javascript, php, sql, text) and the file size in bytes as defined in the $size parameter.

This function checks the default “public” directory for the existence of the files.

$this->drupalCompareFiles($file1, $file2)

$this->checkPermissions(array $permissions, $reset = FALSE)

$this->refreshVariables()

$this->drupalHead($path, array $options = array(), array $headers = array())

This function does a file comparison and returns the differences between the two files.

This function checks to see whether the logged-in user is assigned a set of permissions as defined in the $permissions parameter.

This function resets the defined variables to their initial state.

This function returns only the headers for a Drupal path or an absolute path. The $path parameter defines the URL to load into the internal browser, the $options parameter defines options to be forwarded to the URL, and the $headers parameter contains additional HTTP request headers formatted as name:value.

559

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