Simpletest template

Public

Use it as a guideline for functional testing. Enable Testing module and make sure the .test file is declared in the info file.

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Tests for your projects
  6.  * QA
  7.  */
  8.  
  9. /**
  10.  * Defines a base class for testing the your module.
  11.  */
  12. class YourModuleQA extends DrupalWebTestCase {
  13.  
  14. /**
  15.   * Returns test information.
  16.   *
  17.   * @return array
  18.   * Info
  19.   */
  20. public static function getInfo() {
  21. return array(
  22. 'name' => 'your_module QA tests',
  23. 'description' => 'Test the your_module features to ensure it follows QA requirements.',
  24. 'group' => 'YourModule QA',
  25. );
  26. }
  27.  
  28. /**
  29.   * Test Info File.
  30.   *
  31.   * TestFile <featurename>.info
  32.   */
  33. protected function testInfoFile() {
  34. $module = basename(__FILE__, ".test");
  35. $path = drupal_get_path('module', $module) . '/' . $module . '.info';
  36. $info = drupal_parse_info_file($path);
  37.  
  38. // Add core information.
  39. // Ex core = 7.x.
  40. $this->assertTrue(array_key_exists('core', $info), 'The Drupal core version is available.');
  41. $this->assertEqual($info['core'], '7.x');
  42. }
  43. }
  44. }
  45.  
  46. /**
  47.   * TestInstallFile.
  48.   */
  49. protected function testInstallFile() {
  50. $module = basename(__FILE__, ".test");
  51. $path = drupal_get_path('module', $module) . '/' . $module . '.install';
  52. $data = file_get_contents($path);
  53. $this->assertTrue(strpos($data, $module . '_install'), 'The module contains a hook_install');
  54. $this->assertTrue(strpos($data, $module . '_uninstall'), 'The module contains a hook_uninstall');
  55. $this->assertTrue(strpos($data, $module . '_enable'), 'The module contains a hook_enable');
  56. $this->assertTrue(strpos($data, $module . '_disable'), 'The module contains a hook_disable');
  57. }
  58.  
  59. }
  60.  
  61. /**
  62.  * Defines a base class for testing your module.
  63.  */
  64. class YourModuleDevelopmentQA extends DrupalWebTestCase {
  65.  
  66. protected $adminUser = NULL;
  67. protected $basicUser = NULL;
  68.  
  69.  
  70. /**
  71.   * Returns test information.
  72.   *
  73.   * @return array
  74.   * Info
  75.   */
  76. public static function getInfo() {
  77. return array(
  78. 'name' => 'your feature test',
  79. 'description' => 'Tests your custom feature',
  80. 'group' => 'Custom Functional Tests',
  81. );
  82. }
  83.  
  84. /**
  85.   * Setups the main necessary modules.
  86.   */
  87. public function setUp() {
  88. parent::setUp(array('your_module'));
  89.  
  90. // By pass proxy settings
  91. $https_proxy = array(
  92. 'intragate.yourdomain.edu',
  93. '127.0.0.1',
  94. 'localhost',
  95.  
  96. );
  97. variable_set('proxy_exceptions', $https_proxy);
  98.  
  99. // Create and log in admin user.
  100. $admin_permissions = array(
  101. 'access content',
  102. 'create page content',
  103. 'edit own page content',
  104. 'create article content',
  105. 'edit own article content',
  106. 'administer your feature',
  107. );
  108.  
  109. $this->adminUser = $this->drupalCreateUser($admin_permissions);
  110.  
  111. // Create and log in basic user.
  112. $permissions = array(
  113. 'access content',
  114. 'create page content',
  115. 'edit own page content',
  116. 'create article content',
  117. 'edit own article content',
  118. );
  119.  
  120. $this->basicUser = $this->drupalCreateUser($permissions);
  121. }
  122.  
  123. /**
  124.   * The main test of your_module widget.
  125.   */
  126. public function testYourFeatures() {
  127. $this->loadAdminPageByBasicUser();
  128. $this->saveConfig();
  129. $this->checkContentSetting();
  130. }
  131.  
  132. /**
  133.   * Loads admin page by a basic user.
  134.   */
  135. protected function loadAdminPageByBasicUser() {
  136. $this->drupalLogin($this->basicUser);
  137. $this->drupalGet('admin/config/content/your_module/', array());
  138. $this->assertRaw('Access denied', 'Configuration was not saved!');
  139. $this->drupalLogout();
  140. }
  141.  
  142. /**
  143.   * Saves config for your module.
  144.   */
  145. protected function saveConfig() {
  146. $this->drupalLogin($this->adminUser);
  147. $this->drupalPost('admin/config/content/your_module/', $this->configuration, t('Save configuration'));
  148. $this->assertRaw('The configuration options have been saved', 'Configuration was successfully saved');
  149. $this->drupalLogout();
  150. }
  151.  
  152. /**
  153.   * Checks content settings.
  154.   */
  155. protected function checkContentSetting() {
  156.  
  157. // Check wether basic user has access to override your_module setting.
  158. $this->drupalLogin($this->basicUser);
  159. $this->drupalGet('node/add/article/');
  160. $this->assertNoField('your_custom_field[enabled]', 'No Override settings');
  161.  
  162. $this->drupalLogout();
  163.  
  164. // Check wether admin user has override your_module setting on Article page.
  165. // (article has your_module settings).
  166. $this->drupalLogin($this->adminUser);
  167. $this->drupalGet('node/add/article/');
  168. $this->assertField('your_custom_field[enabled]', 'Override settings');
  169.  
  170. // Check wether admin user has override your_module setting on Basic page.
  171. // (basic page does not have your_module settings).
  172. $this->drupalGet('node/add/page/');
  173. $this->assertNoField('your_custom_field[enabled]', 'No Override settings');
  174. $this->drupalLogout();
  175. }
  176. }

Run your test using Drush

</> CopyGet raw version
php
  1. // from command line where YourModuleQA is your class name
  2. drush test-run YourModuleQA
  3. drush test-clean