Commit 03e6f03a authored by catch's avatar catch

Issue #1594108 by aspilicious: Convert simpletest tests to PSR-0.

parent 66e279cb
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\BrokenSetUpTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests a test case that does not run parent::setUp() in its setUp() method.
*
* If a test case does not call parent::setUp(), running
* Drupal\simpletest\WebTestBase::tearDown() would destroy the main site's
* database tables. Therefore, we ensure that tests which are not set up
* properly are skipped.
*
* @see Drupal\simpletest\WebTestBase
*/
class BrokenSetUpTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Broken SimpleTest method',
'description' => 'Tests a test case that does not call parent::setUp().',
'group' => 'SimpleTest'
);
}
function setUp() {
// If the test is being run from the main site, set up normally.
if (!drupal_valid_test_ua()) {
parent::setUp('simpletest');
// Create and log in user.
$admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($admin_user);
}
// If the test is being run from within simpletest, set up the broken test.
else {
$this->pass(t('The test setUp() method has been run.'));
// Don't call parent::setUp(). This should trigger an error message.
}
}
function tearDown() {
// If the test is being run from the main site, tear down normally.
if (!drupal_valid_test_ua()) {
parent::tearDown();
}
else {
// If the test is being run from within simpletest, output a message.
$this->pass(t('The tearDown() method has run.'));
}
}
/**
* Runs this test case from within the simpletest child site.
*/
function testBreakSetUp() {
// If the test is being run from the main site, run it again from the web
// interface within the simpletest child site.
if (!drupal_valid_test_ua()) {
$edit['Drupal\simpletest\Tests\BrokenSetUpTest'] = TRUE;
$this->drupalPost('admin/config/development/testing', $edit, t('Run tests'));
// Verify that the broken test and its tearDown() method are skipped.
$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.'));
}
// If the test is being run from within simpletest, output a message.
else {
$this->pass(t('The test method has run.'));
}
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\BrowserTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test internal testing framework browser.
*/
class BrowserTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'SimpleTest browser',
'description' => 'Test the internal browser of the testing framework.',
'group' => 'SimpleTest',
);
}
function setUp() {
parent::setUp();
variable_set('user_register', USER_REGISTER_VISITORS);
}
/**
* Test Drupal\simpletest\WebTestBase::getAbsoluteUrl().
*/
function testGetAbsoluteUrl() {
$url = 'user/login';
$this->drupalGet($url);
$absolute = url($url, array('absolute' => TRUE));
$this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
$this->drupalPost(NULL, array(), t('Log in'));
$this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('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, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
}
/**
* Tests XPath escaping.
*/
function testXPathEscaping() {
$testpage = <<< 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->drupalSetContent($testpage);
// Matches the first link.
$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.');
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\FolderTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test Folder creation
*/
class FolderTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Testing SimpleTest setUp',
'description' => "This test will check SimpleTest's treatment of hook_install during setUp. Image module is used for test.",
'group' => 'SimpleTest',
);
}
function setUp() {
return parent::setUp('image');
}
function testFolderSetup() {
$directory = file_default_scheme() . '://styles';
$this->assertTrue(file_prepare_directory($directory, FALSE), "Directory created.");
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\InstallationProfileModuleTestsTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Verifies that tests bundled with installation profile modules are found.
*/
class InstallationProfileModuleTestsTest extends WebTestBase {
/**
* Use the Testing profile.
*
* The Testing profile contains drupal_system_listing_compatible_test.test,
* which attempts to:
* - run tests using the Minimal profile (which does not contain the
* drupal_system_listing_compatible_test.module)
* - but still install the drupal_system_listing_compatible_test.module
* contained in the Testing profile.
*
* @see Drupal\drupal_system_listing_compatible_test\Tests\SystemListingCompatibleTest
*/
protected $profile = 'testing';
public static function getInfo() {
return array(
'name' => 'Installation profile module tests',
'description' => 'Verifies that tests bundled with installation profile modules are found.',
'group' => 'SimpleTest',
);
}
function setUp() {
parent::setUp(array('simpletest'));
$this->admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($this->admin_user);
}
/**
* Tests existence of test case located in an installation profile module.
*/
function testInstallationProfileTests() {
$this->drupalGet('admin/config/development/testing');
$this->assertText('Installation profile module tests helper');
$edit = array(
'Drupal\drupal_system_listing_compatible_test\Tests\SystemListingCompatibleTest' => TRUE,
);
$this->drupalPost(NULL, $edit, t('Run tests'));
$this->assertText('SystemListingCompatibleTest test executed.');
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\MailCaptureTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
class MailCaptureTest extends WebTestBase {
/**
* Implement getInfo().
*/
public static function getInfo() {
return array(
'name' => 'SimpleTest e-mail capturing',
'description' => 'Test the SimpleTest e-mail capturing logic, the assertMail assertion and the drupalGetMails function.',
'group' => 'SimpleTest',
);
}
/**
* Test to see if the wrapper function is executed correctly.
*/
function testMailSend() {
// Create an e-mail.
$subject = $this->randomString(64);
$body = $this->randomString(128);
$message = array(
'id' => 'drupal_mail_test',
'headers' => array('Content-type'=> 'text/html'),
'subject' => $subject,
'to' => 'foobar@example.com',
'body' => $body,
);
// Before we send the e-mail, drupalGetMails should return an empty array.
$captured_emails = $this->drupalGetMails();
$this->assertEqual(count($captured_emails), 0, t('The captured e-mails queue is empty.'), t('E-mail'));
// Send the e-mail.
$response = drupal_mail_system('simpletest', 'drupal_mail_test')->mail($message);
// Ensure that there is one e-mail in the captured e-mails array.
$captured_emails = $this->drupalGetMails();
$this->assertEqual(count($captured_emails), 1, t('One e-mail was captured.'), t('E-mail'));
// Assert that the e-mail was sent by iterating over the message properties
// and ensuring that they are captured intact.
foreach ($message as $field => $value) {
$this->assertMail($field, $value, t('The e-mail was sent and the value for property @field is intact.', array('@field' => $field)), t('E-mail'));
}
// Send additional e-mails so more than one e-mail is captured.
for ($index = 0; $index < 5; $index++) {
$message = array(
'id' => 'drupal_mail_test_' . $index,
'headers' => array('Content-type'=> 'text/html'),
'subject' => $this->randomString(64),
'to' => $this->randomName(32) . '@example.com',
'body' => $this->randomString(512),
);
drupal_mail_system('drupal_mail_test', $index)->mail($message);
}
// There should now be 6 e-mails captured.
$captured_emails = $this->drupalGetMails();
$this->assertEqual(count($captured_emails), 6, t('All e-mails were captured.'), t('E-mail'));
// Test different ways of getting filtered e-mails via drupalGetMails().
$captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test'));
$this->assertEqual(count($captured_emails), 1, t('Only one e-mail is returned when filtering by id.'), t('E-mail'));
$captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject));
$this->assertEqual(count($captured_emails), 1, t('Only one e-mail is returned when filtering by id and subject.'), t('E-mail'));
$captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test', 'subject' => $subject, 'from' => 'this_was_not_used@example.com'));
$this->assertEqual(count($captured_emails), 0, t('No e-mails are returned when querying with an unused from address.'), t('E-mail'));
// Send the last e-mail again, so we can confirm that the drupalGetMails-filter
// correctly returns all e-mails with a given property/value.
drupal_mail_system('drupal_mail_test', $index)->mail($message);
$captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4'));
$this->assertEqual(count($captured_emails), 2, t('All e-mails with the same id are returned when filtering by id.'), t('E-mail'));
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\MissingCheckedRequirementsTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests missing requirements to run test.
*/
class MissingCheckedRequirementsTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Broken requirements test',
'description' => 'Tests a test case with missing requirements.',
'group' => 'SimpleTest',
);
}
function setUp() {
parent::setUp('simpletest');
$admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($admin_user);
}
/**
* Overrides checkRequirements().
*/
protected function checkRequirements() {
if (drupal_valid_test_ua()) {
return array(
'Test is not allowed to run.'
);
}
return parent::checkRequirements();
}
/**
* Ensures test will not run when requirements are missing.
*/
protected function testCheckRequirements() {
// If this is the main request, run the web test script and then assert
// that the child tests did not run.
if (!drupal_valid_test_ua()) {
// Run this test from web interface.
$edit['Drupal\simpletest\Tests\MissingCheckedRequirementsTest'] = TRUE;
$this->drupalPost('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.');
}
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\MissingDependentModuleUnitTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\UnitTestBase;
/**
* Test required modules for tests.
*/
class MissingDependentModuleUnitTest extends UnitTestBase {
public static function getInfo() {
return array(
'name' => 'Testing dependent module test',
'description' => 'This test should not load since it requires a module that is not found.',
'group' => 'SimpleTest',
'dependencies' => array('simpletest_missing_module'),
);
}
/**
* Ensure that this test will not be loaded despite its dependency.
*/
function testFail() {
$this->fail(t('Running test with missing required module.'));
}
}
<?php
/**
* @file
* Definition of Drupal\simpletest\Tests\OtherInstallationProfileModuleTestsTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Verifies that tests in other installation profiles are not found.
*
* @see SimpleTestInstallationProfileModuleTestsTestCase
*/
class OtherInstallationProfileModuleTestsTest extends WebTestBase {
/**
* Use the Minimal profile.
*
* The Testing profile contains drupal_system_listing_compatible_test.test,
* which should not be found.
*
* @see SimpleTestInstallationProfileModuleTestsTestCase
* @see Drupal\drupal_system_listing_compatible_test\Tests\SystemListingCompatibleTest
*/
protected $profile = 'minimal';
public static function getInfo() {
return array(
'name' => 'Other Installation profiles',
'description' => 'Verifies that tests in other installation profiles are not found.',
'group' => 'SimpleTest',
);
}
function setUp() {
parent::setUp(array('simpletest'));
$this->admin_user = $this->drupalCreateUser(array('administer unit tests'));
$this->drupalLogin($this->admin_user);
}
/**
* Tests that tests located in another installation profile do not appear.
*/
function testOtherInstallationProfile() {
$this->drupalGet('admin/config/development/testing');
$this->assertNoText('Installation profile module tests helper');
}
}
......@@ -2,13 +2,15 @@
/**
* @file
* Tests for simpletest.module.
* Definition of Drupal\simpletest\Tests\SimpleTestTest.
*/
namespace Drupal\simpletest\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\simpletest\UnitTestBase;
use SimpleXMLElement;
class SimpleTestFunctionalTest extends WebTestBase {
class SimpleTestTest extends WebTestBase {
/**
* The results array that has been parsed by getTestResults().
*/
......@@ -142,7 +144,7 @@ function testWebTestRunner() {
$this->drupalGet('admin/config/development/testing');
$edit = array();
$edit['SimpleTestFunctionalTest'] = TRUE;
$edit['Drupal\simpletest\Tests\SimpleTestTest'] = TRUE;
$this->drupalPost(NULL, $edit, t('Run tests'));
// Parse results and confirm that they are correct.
......@@ -191,26 +193,26 @@ function assertNothing() {
* 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(t('Enabled modules: %modules', array('%modules' => 'non_existent_module')), 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->setUp()');
$this->assertAssertion($this->pass, 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion($this->fail, 'Other', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion($this->pass, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertAssertion($this->fail, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->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(t('Created permissions: @perms', array('@perms' => $this->valid_permission)), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertAssertion(t('Invalid permission %permission.', array('%permission' => $this->invalid_permission)), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
// Check that a warning is caught by simpletest.
$this->assertAssertion('Division by zero', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion('Division by zero', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
// Check that the backtracing code works for specific assert function.
$this->assertAssertion('This is nothing.', 'Other', 'Pass', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
// Check that errors that occur inside PHP internal functions are correctly reported.
// The exact error message differs between PHP versions so we check only
// the function name 'array_key_exists'.
$this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'simpletest.test', 'SimpleTestFunctionalTest->stubTest()');
$this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
$this->assertEqual('6 passes, 5 fails, 2 exceptions, and 1 debug message', $this->childTestResults['summary'], 'Stub test summary is correct');
......@@ -322,389 +324,3 @@ function inCURL() {
return (bool) drupal_valid_test_ua();
}
}
/**
* Test internal testing framework browser.
*/
class SimpleTestBrowserTestCase extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'SimpleTest browser',
'description' => 'Test the internal browser of the testing framework.',
'group' => 'SimpleTest',
);
}
function setUp() {
parent::setUp();
variable_set('user_register', USER_REGISTER_VISITORS);
}
/**
* Test Drupal\simpletest\WebTestBase::getAbsoluteUrl().
*/
function testGetAbsoluteUrl() {
$url = 'user/login';
$this->drupalGet($url);
$absolute = url($url, array('absolute' => TRUE));
$this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
$this->drupalPost(NULL, array(), t('Log in'));
$this->assertEqual($absolute, $this->url, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('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, t('Passed and requested URL are equal.'));
$this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), t('Requested and returned absolute URL are equal.'));
}
/**
* Tests XPath escaping.
*/
function testXPathEscaping() {
$testpage = <<< 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->drupalSetContent($testpage);
// Matches the first link.
$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.');
}
}
class SimpleTestMailCaptureTestCase extends WebTestBase {
<