- <?php
- * @file
- * Tests for simpletest.module.
- */
-
- class SimpleTestFunctionalTest extends BackdropWebTestCase {
- protected $profile = 'minimal';
-
-
- * The results array that has been parsed by getTestResults().
- */
- protected $childTestResults;
-
-
- * @var string
- */
- protected $pass;
-
-
- * @var string
- */
- protected $fail;
-
-
- * @var string
- */
- protected $valid_permission;
-
-
- * @var string
- */
- protected $invalid_permission;
-
-
- * Store the test ID from each test run for comparison, to ensure they are
- * incrementing.
- */
- protected $test_ids = array();
-
- function setUp() {
- if (!$this->inCURL()) {
- parent::setUp('simpletest');
-
-
- $admin_user = $this->backdropCreateUser(array('administer unit tests'));
- $this->backdropLogin($admin_user);
- }
- else {
- parent::setUp('non_existent_module');
- }
- }
-
-
- * Test the internal browsers functionality.
- */
- function testInternalBrowser() {
- if (!$this->inCURL()) {
- $this->backdropGet('node');
- $this->assertTrue($this->backdropGetHeader('Date'), 'An HTTP header was received.');
- $this->assertTitle(t('Welcome to @site-name | @site-name', array('@site-name' => config_get('system.core', 'site_name'))), 'Site title matches.');
- $this->assertNoTitle('Foo', 'Site title does not match.');
-
-
- global $base_url;
-
- $this->backdropGet($base_url . '/core/install.php', array('external' => TRUE));
- $this->assertResponse(403, 'Cannot access install.php with a "simpletest" user-agent header.');
-
- $user = $this->backdropCreateUser();
- $this->backdropLogin($user);
- $headers = $this->backdropGetHeaders(TRUE);
- $this->assertEqual(count($headers), 2, 'There was one intermediate request.');
- $this->assertTrue(strpos($headers[0][':status'], '302') !== FALSE, 'Intermediate response code was 302.');
- $this->assertFalse(empty($headers[0]['location']), 'Intermediate request contained a Location header.');
- $this->assertEqual($this->getUrl(), $headers[0]['location'], 'HTTP redirect was followed');
- $this->assertFalse($this->backdropGetHeader('Location'), 'Headers from intermediate request were reset.');
- $this->assertResponse(200, 'Response code from intermediate request was reset.');
-
-
- $layout = layout_load('default');
- $layout->addBlock('user', 'login', 'sidebar');
- $layout->save();
-
-
- $this->backdropLogout();
- $edit = array(
- 'name' => $user->name,
- 'pass' => $user->pass_raw
- );
- $this->maximumRedirects = 1;
- $this->backdropPost('user?destination=user/logout', $edit, t('Log in'));
- $headers = $this->backdropGetHeaders(TRUE);
- $this->assertEqual(count($headers), 2, 'Simpletest stopped following redirects after the first one.');
- }
- }
-
-
- * Test validation of the User-Agent header we use to perform test requests.
- */
- function testUserAgentValidation() {
- if (!$this->inCURL()) {
- global $base_url;
- $simpletest_path = $base_url . '/' . backdrop_get_path('module', 'simpletest');
- $HTTP_path = $simpletest_path .'/tests/http.php?q=node';
- $https_path = $simpletest_path .'/tests/https.php?q=node';
-
- $this->assertTrue(preg_match('/simpletest\d+/', $this->databasePrefix, $matches), 'Database prefix contains simpletest prefix.');
- $test_ua = backdrop_generate_test_ua($matches[0]);
- $this->additionalCurlOptions = array(CURLOPT_USERAGENT => $test_ua);
-
-
- $this->backdropGet($HTTP_path);
- $this->assertResponse(200, 'Requesting http.php with a legitimate simpletest User-Agent returns OK.');
- $this->backdropGet($https_path);
- $this->assertResponse(200, 'Requesting https.php with a legitimate simpletest User-Agent returns OK.');
-
-
- $this->additionalCurlOptions = array(CURLOPT_USERAGENT => $test_ua . 'X');
- $this->backdropGet($HTTP_path);
- $this->assertResponse(403, 'Requesting http.php with a bad simpletest User-Agent fails.');
- $this->backdropGet($https_path);
- $this->assertResponse(403, 'Requesting https.php with a bad simpletest User-Agent fails.');
-
-
-
- $this->additionalCurlOptions = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
- $this->backdropGet($HTTP_path);
- $this->assertResponse(403, 'Requesting http.php with a normal User-Agent fails.');
- $this->backdropGet($https_path);
- $this->assertResponse(403, 'Requesting https.php with a normal User-Agent fails.');
- }
- }
-
-
- * Make sure that tests selected through the web interface are run and
- * that the results are displayed correctly.
- */
- function testWebTestRunner() {
- $this->pass = t('SimpleTest pass.');
- $this->fail = t('SimpleTest fail.');
- $this->valid_permission = 'access content';
- $this->invalid_permission = 'invalid permission';
-
- if ($this->inCURL()) {
-
- $this->stubTest();
- }
- else {
-
- $this->backdropGet('admin/config/development/testing');
-
- $edit = array();
- $edit['tests[SimpleTestFunctionalTest]'] = TRUE;
- $this->backdropPost(NULL, $edit, t('Run tests'));
-
-
- $this->getTestResults();
- $this->confirmStubTestResults();
- }
- }
-
-
- * Test to be run and the results confirmed.
- */
- function stubTest() {
- $this->pass($this->pass);
- $this->fail($this->fail);
-
- $this->backdropCreateUser(array($this->valid_permission));
- $this->backdropCreateUser(array($this->invalid_permission));
-
- $this->pass(t('Test ID is @id.', array('@id' => $this->testId)));
-
-
- $a = '';
- foreach ($a as $b) {
-
- }
-
-
- $this->assertNothing();
-
-
- simplexml_load_string('<fake>');
-
- debug('Foo', 'Debug');
- }
-
-
- * Assert nothing.
- */
- function assertNothing() {
- $this->pass("This is nothing.");
- }
-
-
- * Confirm that the stub test produced the desired results.
- */
- function confirmStubTestResults() {
- $this->assertAssertion(t('Enabled modules: %modules', array('%modules' => 'non_existent_module')), 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->setUp()');
-
- $this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
- $this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
- $this->assertAssertion(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
- $this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
-
-
-
- $this->assertAssertion('foreach()', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
-
- $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
-
-
-
- $this->assertAssertion('simplexml_load_string', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
-
-
- $acceptable_results = array(
- '6 passes, 5 fails, 4 exceptions, and 1 debug message.',
- '6 passes, 5 fails, 2 exceptions, and 1 debug message.',
- );
- $summary_correct = in_array($this->childTestResults['summary'], $acceptable_results);
- $this->assertTrue($summary_correct, 'Stub test summary is correct');
-
- $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
-
- $this->test_ids[] = $test_id = $this->getTestIdFromResults();
- $this->assertTrue($test_id, 'Found test ID in results.');
- }
-
-
- * Fetch the test id from the test results.
- */
- function getTestIdFromResults() {
- foreach ($this->childTestResults['assertions'] as $assertion) {
- if (preg_match('@^PASS: Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
- return $matches[1];
- }
- }
- return NULL;
- }
-
-
- * Assert that an assertion with the specified values is displayed
- * in the test results.
- *
- * @param string $message
- * Assertion message.
- * @param string $type
- * Assertion type.
- * @param string $status
- * Assertion status.
- * @param string $file
- * File where the assertion originated.
- * @param string $function
- * Function where the assertion originated.
- * @return boolean
- * TRUE if the assertion is displayed, FALSE if it is not.
- */
- function assertAssertion($message, $type, $status, $file, $function) {
- $message = trim(strip_tags($message));
- $found = FALSE;
- foreach ($this->childTestResults['assertions'] as $assertion) {
- if ((strpos($assertion['message'], $message) !== FALSE) &&
- $assertion['type'] == $type &&
- $assertion['status'] == $status &&
- $assertion['file'] == $file &&
- $assertion['function'] == $function) {
- $found = TRUE;
- break;
- }
- }
- return $this->assertTrue($found, format_string('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', array('@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function)));
- }
-
-
- * Get the results from a test and store them in the class array $results.
- */
- function getTestResults() {
- $results = array();
- if ($this->parse()) {
- if ($fieldset = $this->getResultFieldSet()) {
-
- $results['summary'] = $this->asText($fieldset->div->div[1]);
- $results['name'] = $this->asText($fieldset->legend);
-
- $results['assertions'] = array();
- $tbody = $fieldset->div->table->tbody;
- foreach ($tbody->tr as $row) {
- $assertion = array();
- $assertion['message'] = $this->asText($row->td[0]);
- $assertion['type'] = $this->asText($row->td[1]);
- $assertion['file'] = $this->asText($row->td[2]);
- $assertion['line'] = $this->asText($row->td[3]);
- $assertion['function'] = $this->asText($row->td[4]);
- $ok_url = file_create_url('core/misc/watchdog-ok.png');
- $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
- $results['assertions'][] = $assertion;
- }
- }
- }
- $this->childTestResults = $results;
- }
-
-
- * Get the fieldset containing the results for group this test is in.
- */
- function getResultFieldSet() {
- $fieldsets = $this->xpath('//fieldset');
- $info = simpletest_test_get_by_class(get_class($this));
- foreach ($fieldsets as $fieldset) {
- if ($this->asText($fieldset->legend) == $info['name']) {
- return $fieldset;
- }
- }
- return FALSE;
- }
-
-
- * Extract the text contained by the element.
- *
- * @param $element
- * Element to extract text from.
- * @return
- * Extracted text.
- */
- function asText(SimpleXMLElement $element) {
- if (!is_object($element)) {
- return $this->fail('The element is not an element.');
- }
- return trim(html_entity_decode(strip_tags($element->asXML())));
- }
-
-
- * Check if the test is being run from inside a CURL request.
- */
- function inCURL() {
- return (bool) backdrop_valid_test_ua();
- }
- }
-
- * Test internal testing framework browser.
- */
- class SimpleTestBrowserTestCase extends BackdropWebTestCase {
-
- * A flag indicating whether a cookie has been set in a test.
- *
- * @var bool
- */
- protected static $cookieSet = FALSE;
-
- function setUp() {
- parent::setUp();
- config_set('system.core', 'user_register', USER_REGISTER_VISITORS);
- }
-
-
- * Test BackdropWebTestCase::getAbsoluteUrl().
- */
- function testGetAbsoluteUrl() {
-
- config_set('system.core', 'clean_url', 0);
- backdrop_static_reset('url');
- $url = 'user/login';
-
- $this->backdropGet($url);
- $absolute = url($url, array('absolute' => TRUE));
- $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
- $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
-
- $this->backdropPost(NULL, array(), t('Log in'));
- $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
- $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
-
- $this->clickLink('Create new account');
- $url = 'user/register';
- $absolute = url($url, array('absolute' => TRUE));
- $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
- $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
- }
-
-
- * Tests XPath escaping.
- */
- function testXPathEscaping() {
- $test_page = <<< EOF
- <html>
- <body>
- <a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
- <a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
- </body>
- </html>
- EOF;
- $this->backdropSetContent($test_page);
-
-
- $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"'));
- $this->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.');
-
- $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A second "even more weird" link, in memory of George O\'Malley'));
- $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
- }
-
-
- * Tests that cookies set during a request are available for testing.
- */
- public function testCookies() {
-
- $user = $this->backdropCreateUser();
- $edit = array('name' => $user->name, 'pass' => $user->pass_raw);
- $this->backdropPost('user/login', $edit, t('Log in'));
- $this->assertEqual(count($this->cookies), 1, 'A cookie is set when the user logs in.');
-
-
- $cookie_header = $this->backdropGetHeader('set-cookie', TRUE);
-
-
-
- preg_match('/^([^=]+)=([^;]+)/', $cookie_header, $matches);
- $name = $matches[1];
- $value = $matches[2];
-
- $this->assertTrue(array_key_exists($name, $this->cookies), 'The cookie name is correct.');
- $this->assertEqual($value, $this->cookies[$name]['value'], 'The cookie value is correct.');
-
-
-
- self::$cookieSet = TRUE;
- }
-
-
- * Tests that the cookies from a previous test do not bleed into a new test.
- *
- * @see SimpleTestBrowserTestCase::testCookies().
- */
- public function testCookieDoesNotBleed() {
-
-
- $this->assertTrue(self::$cookieSet, 'Tests have been executed in the expected order.');
- $this->assertEqual(count($this->cookies), 0, 'No cookies are present at the start of a new test.');
- }
- }
-
- class SimpleTestMailCaptureTestCase extends BackdropWebTestCase {
-
- * Test to see if the wrapper function is executed correctly.
- */
- function testMailSend() {
-
- $subject = $this->randomString(64);
- $body = $this->randomString(128);
- $message = array(
- 'id' => 'backdrop_mail_test',
- 'headers' => array('Content-type'=> 'text/html'),
- 'subject' => $subject,
- 'to' => 'foobar@example.com',
- 'body' => $body,
- );
-
-
- $captured_emails = $this->backdropGetMails();
- $this->assertEqual(count($captured_emails), 0, 'The captured emails queue is empty.', 'Email');
-
-
- $response = backdrop_mail_system('simpletest', 'backdrop_mail_test')->mail($message);
-
-
- $captured_emails = $this->backdropGetMails();
- $this->assertEqual(count($captured_emails), 1, 'One email was captured.', 'Email');
-
-
-
- foreach ($message as $field => $value) {
- $this->assertMail($field, $value, format_string('The email was sent and the value for property @field is intact.', array('@field' => $field)), 'Email');
- }
-
-
- for ($index = 0; $index < 5; $index++) {
- $message = array(
- 'id' => 'backdrop_mail_test_' . $index,
- 'headers' => array('Content-type'=> 'text/html'),
- 'subject' => $this->randomString(64),
- 'to' => $this->randomName(32) . '@example.com',
- 'body' => $this->randomString(512),
- );
- backdrop_mail_system('backdrop_mail_test', $index)->mail($message);
- }
-
-
- $captured_emails = $this->backdropGetMails();
- $this->assertEqual(count($captured_emails), 6, 'All emails were captured.', 'Email');
-
-
- $captured_emails = $this->backdropGetMails(array('id' => 'backdrop_mail_test'));
- $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id.', 'Email');
- $captured_emails = $this->backdropGetMails(array('id' => 'backdrop_mail_test', 'subject' => $subject));
- $this->assertEqual(count($captured_emails), 1, 'Only one email is returned when filtering by id and subject.', 'Email');
- $captured_emails = $this->backdropGetMails(array('id' => 'backdrop_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com'));
- $this->assertEqual(count($captured_emails), 0, 'No emails are returned when querying with an unused from address.', 'Email');
-
-
-
- backdrop_mail_system('backdrop_mail_test', $index)->mail($message);
- $captured_emails = $this->backdropGetMails(array('id' => 'backdrop_mail_test_4'));
- $this->assertEqual(count($captured_emails), 2, 'All emails with the same id are returned when filtering by id.', 'Email');
- }
- }
-
- * Test Folder creation
- */
- class SimpleTestFolderTestCase extends BackdropWebTestCase {
- protected $profile = 'testing';
-
- function setUp() {
- return parent::setUp('image');
- }
-
- function testFolderSetup() {
- $directory = file_default_scheme() . '://styles';
- $this->assertTrue(file_prepare_directory($directory, FALSE), 'Directory created.');
- }
- }
-
- * Test required modules for tests.
- */
- class SimpleTestMissingDependentModuleUnitTest extends BackdropUnitTestCase {
-
- * Ensure that this test will not be loaded despite its dependency.
- */
- function testFail() {
- $this->fail(t('Running test with missing required module.'));
- }
- }
-
- * Tests a test case that does not run parent::setUp() in its setUp() method.
- *
- * If a test case does not call parent::setUp(), running
- * BackdropTestCase::tearDown() would destroy the main site's database tables.
- * Therefore, we ensure that tests which are not set up properly are skipped.
- *
- * @see BackdropTestCase
- */
- class SimpleTestBrokenSetUp extends BackdropWebTestCase {
- function setUp() {
-
- if (!backdrop_valid_test_ua()) {
- parent::setUp('simpletest');
-
- $admin_user = $this->backdropCreateUser(array('administer unit tests'));
- $this->backdropLogin($admin_user);
- }
-
- else {
- $this->pass(t('The test setUp() method has been run.'));
-
- }
- }
-
- function tearDown() {
-
- if (!backdrop_valid_test_ua()) {
- parent::tearDown();
- }
- else {
-
- $this->pass(t('The tearDown() method has run.'));
- }
- }
-
-
- * Runs this test case from within the simpletest child site.
- */
- function testBreakSetUp() {
-
-
- if (!backdrop_valid_test_ua()) {
- $edit['tests[SimpleTestBrokenSetUp]'] = TRUE;
- $this->backdropPost('admin/config/development/testing', $edit, t('Run tests'));
-
-
- $this->assertRaw(t('The test setUp() method has been run.'));
- $this->assertRaw(t('The test cannot be executed because it has not been set up properly.'));
- $this->assertNoRaw(t('The test method has run.'));
- $this->assertNoRaw(t('The tearDown() method has run.'));
- }
-
- else {
- $this->pass(t('The test method has run.'));
- }
- }
- }
-
- * Tests missing requirements to run test.
- */
- class SimpleTestMissingCheckedRequirements extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('simpletest');
- $admin_user = $this->backdropCreateUser(array('administer unit tests'));
- $this->backdropLogin($admin_user);
- }
-
-
- * Overrides checkRequirements().
- */
- protected function checkRequirements() {
- $errors = parent::checkRequirements();
-
- if (backdrop_valid_test_ua()) {
- $errors[] = 'Test is not allowed to run.';
- }
- return $errors;
- }
-
-
- * Ensures test will not run when requirements are missing.
- */
- protected function testCheckRequirements() {
-
-
- if (!backdrop_valid_test_ua()) {
-
- $edit['tests[SimpleTestMissingCheckedRequirements]'] = TRUE;
- $this->backdropPost('admin/config/development/testing', $edit, t('Run tests'));
- $this->assertRaw('Test is not allowed to run.', 'Test check for requirements came up.');
- $this->assertNoText('Test ran when it failed requirements check.', 'Test requirements stopped test from running.');
- }
- else {
- $this->fail('Test ran when it failed requirements check.');
- }
- }
- }
-
- * Verifies that tests bundled with installation profile modules are found.
- */
- class SimpleTestInstallationProfileModuleTestsTestCase extends BackdropWebTestCase {
-
- * Use the Testing profile.
- *
- * The Testing profile contains backdrop_system_listing_compatible_test.test,
- * which attempts to:
- * - run tests using the Minimal profile (which does not contain the
- * backdrop_system_listing_compatible_test.module)
- * - but still install the backdrop_system_listing_compatible_test.module
- * contained in the Testing profile.
- *
- * @see BackdropSystemListingCompatibleTestCase
- */
- protected $profile = 'testing';
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp(array('simpletest'));
-
- $this->admin_user = $this->backdropCreateUser(array('administer unit tests'));
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Tests existence of test case located in an installation profile module.
- */
- function testInstallationProfileTests() {
- $this->backdropGet('admin/config/development/testing');
- $this->assertText('Installation profile module tests helper');
- $edit = array(
- 'tests[BackdropSystemListingCompatibleTestCase]' => TRUE,
- );
- $this->backdropPost(NULL, $edit, t('Run tests'));
- $this->assertText('BackdropSystemListingCompatibleTestCase test executed.');
- }
- }
-
- * Verifies that tests in other installation profiles are not found.
- *
- * @see SimpleTestInstallationProfileModuleTestsTestCase
- */
- class SimpleTestOtherInstallationProfileModuleTestsTestCase extends BackdropWebTestCase {
-
- * Use the Minimal profile.
- *
- * The Testing profile contains backdrop_system_listing_compatible_test.test,
- * which should not be found.
- *
- * @see SimpleTestInstallationProfileModuleTestsTestCase
- * @see BackdropSystemListingCompatibleTestCase
- */
- protected $profile = 'minimal';
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp(array('simpletest'));
-
- $this->admin_user = $this->backdropCreateUser(array('administer unit tests'));
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Tests that tests located in another installation profile do not appear.
- */
- function testOtherInstallationProfile() {
- $this->backdropGet('admin/config/development/testing');
- $this->assertNoText('Installation profile module tests helper');
- }
- }