00001 <?php
00002
00003
00011 class SimpleTestFunctionalTest extends DrupalWebTestCase {
00015 protected $childTestResults;
00016
00021 protected $test_ids = array();
00022
00023 public static function getInfo() {
00024 return array(
00025 'name' => t('SimpleTest functionality'),
00026 'description' => t('Test SimpleTest\'s web interface: check that the intended tests were
00027 run and ensure that test reports display the intended results. Also
00028 test SimpleTest\'s internal browser and API\'s both explicitly and
00029 implicitly.'),
00030 'group' => t('SimpleTest')
00031 );
00032 }
00033
00034 function setUp() {
00035 if (!$this->inCURL()) {
00036 parent::setUp('simpletest');
00037
00038
00039 $admin_user = $this->drupalCreateUser(array('administer unit tests'));
00040 $this->drupalLogin($admin_user);
00041 }
00042 else {
00043 parent::setUp();
00044 }
00045 }
00046
00050 function testInternalBrowser() {
00051 global $conf;
00052 if (!$this->inCURL()) {
00053 $this->drupalGet('node');
00054 $this->assertTrue($this->drupalGetHeader('Date'), t('An HTTP header was received.'));
00055 $this->assertTitle(variable_get('site_name', 'Drupal'), t('Site title matches.'));
00056 $this->assertNoTitle('Foo', t('Site title does not match.'));
00057
00058
00059 global $base_url;
00060
00061 $this->drupalGet($base_url . '/install.php', array('external' => TRUE));
00062 $this->assertResponse(403, 'Cannot access install.php with a "simpletest" user-agent header.');
00063
00064 $this->drupalLogin($this->drupalCreateUser());
00065 $headers = $this->drupalGetHeaders(TRUE);
00066 $this->assertEqual(count($headers), 2, t('There was one intermediate request.'));
00067 $this->assertTrue(strpos($headers[0][':status'], '302') !== FALSE, t('Intermediate response code was 302.'));
00068 $this->assertFalse(empty($headers[0]['location']), t('Intermediate request contained a Location header.'));
00069 $this->assertEqual($this->getUrl(), $headers[0]['location'], t('HTTP redirect was followed'));
00070 $this->assertFalse($this->drupalGetHeader('Location'), t('Headers from intermediate request were reset.'));
00071 $this->assertResponse(200, t('Response code from intermediate request was reset.'));
00072 }
00073 }
00074
00079 function testWebTestRunner() {
00080 $this->pass = t('SimpleTest pass.');
00081 $this->fail = t('SimpleTest fail.');
00082 $this->valid_permission = 'access content';
00083 $this->invalid_permission = 'invalid permission';
00084
00085 if ($this->inCURL()) {
00086
00087 $this->stubTest();
00088 }
00089 else {
00090
00091
00092 for ($i = 0; $i < 2; $i++) {
00093
00094 $this->drupalGet('admin/build/testing');
00095
00096 $edit = array();
00097 $edit['SimpleTestFunctionalTest'] = TRUE;
00098 $this->drupalPost(NULL, $edit, t('Run tests'));
00099
00100
00101 $this->getTestResults();
00102 $this->confirmStubTestResults();
00103 }
00104
00105
00106
00107 $this->assertTrue($this->test_ids[0] != $this->test_ids[1], t('Test ID is incrementing.'));
00108 }
00109 }
00110
00114 function stubTest() {
00115 $this->pass($this->pass);
00116 $this->fail($this->fail);
00117
00118 $this->drupalCreateUser(array($this->valid_permission));
00119 $this->drupalCreateUser(array($this->invalid_permission));
00120
00121 $this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
00122
00123
00124 $i = 1 / 0;
00125
00126
00127 $this->assertNothing();
00128
00129
00130 array_key_exists(NULL, NULL);
00131 }
00132
00136 function assertNothing() {
00137 $this->pass("This is nothing.");
00138 }
00139
00143 function confirmStubTestResults() {
00144 $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00145 $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00146
00147 $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00148 $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00149
00150
00151 $this->assertAssertion('Division by zero', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00152
00153
00154 $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00155
00156
00157
00158
00159 $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
00160
00161 $this->test_ids[] = $test_id = $this->getTestIdFromResults();
00162 $this->assertTrue($test_id, t('Found test ID in results.'));
00163 }
00164
00168 function getTestIdFromResults() {
00169 foreach($this->childTestResults['assertions'] as $assertion) {
00170 if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
00171 return $matches[1];
00172 }
00173 }
00174 return NULL;
00175 }
00176
00188 function assertAssertion($message, $type, $status, $file, $function) {
00189 $message = trim(strip_tags($message));
00190 $found = FALSE;
00191 foreach ($this->childTestResults['assertions'] as $assertion) {
00192 if ((strpos($assertion['message'], $message) !== FALSE) &&
00193 $assertion['type'] == $type &&
00194 $assertion['status'] == $status &&
00195 $assertion['file'] == $file &&
00196 $assertion['function'] == $function) {
00197 $found = TRUE;
00198 break;
00199 }
00200 }
00201 return $this->assertTrue($found, t('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', array('@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function)));
00202 }
00203
00207 function getTestResults() {
00208 $results = array();
00209
00210 if ($this->parse()) {
00211 if ($fieldset = $this->getResultFieldSet()) {
00212
00213 $results['summary'] = $this->asText($fieldset->div);
00214 $results['name'] = $this->asText($fieldset->fieldset->legend);
00215
00216 $results['assertions'] = array();
00217 $tbody = $fieldset->fieldset->table->tbody;
00218 foreach ($tbody->tr as $row) {
00219 $assertion = array();
00220 $assertion['message'] = $this->asText($row->td[0]);
00221 $assertion['type'] = $this->asText($row->td[1]);
00222 $assertion['file'] = $this->asText($row->td[2]);
00223 $assertion['line'] = $this->asText($row->td[3]);
00224 $assertion['function'] = $this->asText($row->td[4]);
00225 $ok_url = (url('misc/watchdog-ok.png') == 'misc/watchdog-ok.png') ? 'misc/watchdog-ok.png' : (base_path() . 'misc/watchdog-ok.png');
00226 $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
00227 $results['assertions'][] = $assertion;
00228 }
00229 }
00230 }
00231 $this->childTestResults = $results;
00232 }
00233
00239 function getResultFieldSet() {
00240 $fieldsets = $this->xpath('//fieldset');
00241 $info = $this->getInfo();
00242 foreach ($fieldsets as $fieldset) {
00243 if ($fieldset->legend == $info['group']) {
00244 return $fieldset;
00245 }
00246 }
00247 return FALSE;
00248 }
00249
00258 function asText(SimpleXMLElement $element) {
00259 if (!is_object($element)) {
00260 return $this->fail('The element is not an element.');
00261 }
00262 return trim(html_entity_decode(strip_tags($element->asXML())));
00263 }
00264
00270 function inCURL() {
00271 return preg_match("/^simpletest\d+/", $_SERVER['HTTP_USER_AGENT']);
00272 }
00273
00274
00331
00332
00333
00334
00335 function testUserAccess()
00336 {
00337 $user = $this->drupalCreateUser(array('administer unit tests'));
00338 $this->assertTrue(user_access('administer unit tests', $user), 'user_access() check successfull.');
00339 }
00340
00341 function testUserAccessCache()
00342 {
00343 $user = $this->drupalCreateUser(array('administer nodes'));
00344 $this->assertTrue(user_access('administer nodes', $user), 'user_access() cache has been cleaned');
00345
00346 }
00347 }