You have added features to the site a few days later the customer called and told me that nothing works. You have 20 hours stuffing code and a bunch of times and go on klatsaete forms of testing to get everything working, but the brain perceives nothing and end up on the site added a broken piece. Or maybe you have a complex module with a bunch of interconnected functional, well or small, but with a lot of choices. In general, you have millions of reasons to come to the automated testing.
Use of automated testing can get rid of piles of routine operations on a regular functional testing code. To test the available AutoComplete forms and checking the result, control user access to various sections of the site and functionality, and much more.
Testing modules and functionality in Drupal module by using SimpleTest. And, with version 7 is included in the kernel, so look the other way and do not make much sense.
To install you need to set Drupal and that the server was accessible library php-curl, with which the module implements parsing of pages.
Once the module is copied to the server, you need to patch the kernel, the patch file is located in the root folder of the module. To use it you must click on the server from the root of the site:
patch-p0 <{path-to-folder-with-modules} / simpletest/D6-core-simpletest.patch
Then you can just activate it in the tab with modules and you can view a list of available tests on the page admin / build / testing.
In the beginning, it scans the folder modules in search of available tests, if it does not matter whether or not the module is active, the user sees and tests themselves and can not fulfill them including the corresponding module.
This is achieved thanks to the fact that, before starting the test SimpleTest creates a virtual installation of Drupal with a later and running. Already it activates the necessary modules and themes, which may differ from the installation of the current site.
Further, in the testing process tested caused a page or function, after which they validate and generate output information about the success or failure of the operation.
By the way, for each function testXXX setUp runs every time before performing the test.
Thus, we conclude the flood and move on to practice. In the first test we examine the creation of material such as Page, which is available in all installations. To do this we need:
Create a test file with the name imya_modulya.test and save it in the folder with the module. The file name specified in the tough SimpleTest.
Next, create the test itself:
<?php
class OurModuleTest extends DrupalWebTestCase {
/ / Helper function, which we will generate a text with blanks and Blackjack
protected function randomText ($ wordCount = 32) {
$ Text =”;
for ($ i = 0; $ i <$ wordCount; $ i + +) {
$ Text .= $ this-> randomString (rand (4, 12)). ”;
}
return $ text;
}
/ / Information about the test, which is displayed on the page of tests.
public static function getInfo () {
return array (
‘Name’ => ‘Page creation test’,
‘Desc’ => ‘Testing page creation’,
‘Group’ => ‘Our tests’,
);
}
public function setUp () {
/ / Set the required modules
$ Args = func_get_args ();
$ Modules = array_merge (array (’help’, ’search’, ‘menu’, ‘node’), $ args);
call_user_func_array (array (’parent’, ’setUp’), $ modules);
/ / Set the right user permissions
$ Permissions = array (’access content’, ‘create page content’, ‘delete own page content’, ‘edit own page content’);
/ / Create a user with these rights and enter into the system
$ User = $ this-> drupalCreateUser ($ permissions);
$ This-> drupalLogin ($ user);
}
/ / Testing a page
public function testPageCreation () {
$ Params = array (
‘Title’ => $ this-> randomName (32)
‘Body’ => $ this-> randomText (),
);
/ / Call a Page Page
$ This-> drupalPost (’node / add / page’, $ params, t (’Save’));
/ / Check if the received input
$ This-> assertText (t (’Page @ name has been created.’, Array (’@ name’ => $ params ['title'])), t (’ Page creation ‘));
}
}
?>
Clear the cache and go to the page admin / build / testing. Now, there we see the drop-down tab "Our tests", which is available in a single test "Page creation test". By putting a tick on him to fulfill it. after the information is available to us "19 passes, 0 fails, and 0 exceptions". The fact that we wanted.
Now log out the user and then try to perform the test. To do this, create another test and call it testAnonymousPageCreation. From the previous test code will be different only in that we run before running $ this-> drupalLogout ()
/ / Test create page anonymous user
public function testAnonymousPageCreation () {
/ / User Razloginivaem
$ This-> drupalLogout ();
$ Params = array (
‘Title’ => $ this-> randomName (32)
‘Body’ => $ this-> randomText (),
);
/ / Call a Page Page
$ This-> drupalPost (’node / add / page’, $ params, t (’Save’));
/ / Check if the received input
$ This-> assertText (t (’Page @ name has been created.’, Array (’@ name’ => $ params ['title'])), t (’ Page creation ‘));
}
Now the result of 29 passes, 5 fails, and 0 exceptions. However, this is not the result, which was worth getting. In this case, you need to check whether the user’s access is blocked to this page, this will be a successful test, this will modify the test:
/ / Test create page anonymous user
public function testAnonymousPageCreation () {
/ / User Razloginivaem
$ This-> drupalLogout ();
/ / Try to get the desired page
$ This-> drupalGet (’node / add / page’);
/ / Check the server response to error 403 (Access denied)
$ This-> assertResponse (403, t (’You have no permitions to this page .’));
}
Now the result: 30 passes, 0 fails, and 0 exceptions. Ok, now we know that unauthorized users can not get access to the creation of pages.
Next we have to teach myself to write code right away with the tests.SimpleTest provides enough functionality to solve many problems.
There is a set of small and major problems and issues related to testing with the help of this module, which you or come across or not, but we warn you:)
SimpleTest can not test JavaScript, therefore the functional jQuery, dynamic content spoofing, etc. test will not work :(
The list of available tests (Assertions) is available here: http://drupal.org/node/265828
To form the module must be called View $ this-> drupalGet (), instead of drupalPost (). Example:
$ Params = array (’sorting’ => ’sorting_value’);
$ This-> drupalGet (’find / wine-ratings’, array (’query’ => $ params));
Tests are available for inactive modules.
Establishment types, etc. should be brought to a separate module, and write all the necessary procedures in module_name.install.
If you create a separate module for testing, then the file should be added module_name.info hidden = TRUE, then the module can be invoked in the tests, but will not be available in the list.
Nodecomment module conflicts with the module comment, so you should edit the profiles \ default \ default.profile and remove it from the default settings.
And finally an extended version of the class DrupalWebTestCase, which added additional functions and features:
class ExtendedDrupalWebTestCase extends DrupalWebTestCase {
protected $ admin_user;
protected $ users;
/ / Helper function, which we will generate a text with blanks and Blackjack
protected function randomText ($ wordCount = 32) {
$ Text =”;
for ($ i = 0; $ i <$ wordCount; $ i + +) {
$ Text .= $ this-> randomString (rand (4, 12)). ”;
}
return $ text;
}
/ / Change the current theme
protected function setTheme ($ new_theme) {
db_query ("UPDATE {system} SET status = 1 WHERE type = ‘theme’ and name = ‘% s’", $ new_theme);
variable_set (’theme_default’, $ new_theme);
drupal_rebuild_theme_registry ();
}
/ / Generate file name to display a folder that is outside the temporary folder and SimpleTest can view the data after cleaning.
protected function getOutputFile () {
$ File_dir = file_directory_path ();
$ File_dir .= ‘. / Simpletest_output_pages’;
if (! is_dir ($ file_dir)) {
mkdir ($ file_dir, 0777, TRUE);
}
return "$ file_dir / $ basename." . $ This-> randomName (10). ‘. Html’;
}
/ / Write page
protected function outputAdminPage ($ description, $ basename, $ url) {
$ Output_path = $ this-> getOutputFile ();
$ This-> drupalGet ($ url);
$ Rv = file_put_contents ($ output_path, $ this-> drupalGetContent ());
$ This-> pass ("$ description: Contents of result page are". L (’here’, $ output_path));
}
/ / Write the last on-screen display
protected function outputScreenContents ($ description, $ basename) {
$ Output_path = $ this-> getOutputFile ();
$ Rv = file_put_contents ($ output_path, $ this-> drupalGetContent ());
$ This-> pass ("$ description: Contents of result page are". L (’here’, $ output_path));
}
/ / Write variable to file
protected function outputVariable ($ description, $ variable) {
$ Output_path = $ this-> getOutputFile ();
$ Rv = file_put_contents ($ output_path, ‘<html> <body> <pre>’. Print_r ($ variable, true ).’</ pre> </ body> </ html> ‘);
$ This-> pass ("$ description: Contents of result page are". L (’here’, $ output_path));
}
}