Commit 4710b097 authored by alexpott's avatar alexpott
Browse files

Issue #2542486 by neclimdul, Wim Leers, Mile23: Fix risky phpunit tests

parent 2c15ad9e
......@@ -218,6 +218,7 @@ public function testGetAbsoluteUrl($href, $expected_absolute_path) {
$get_absolute_url_method->setAccessible(TRUE);
$this->assertSame($expected_absolute_path, $get_absolute_url_method->invoke($web_test, $href));
unset($GLOBALS['base_url'], $GLOBALS['base_path']);
}
/**
......
......@@ -68,15 +68,12 @@ protected function findPhpClasses($dir) {
*/
protected function assertNoCoreUsage($class_path) {
$contents = file_get_contents($class_path);
if (preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches)) {
foreach ($matches[0] as $line) {
if ((strpos($line, '@see ') === FALSE)) {
$this->fail(
"Illegal reference to 'Drupal\\Core' namespace in $class_path"
);
}
}
}
preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches);
$matches = array_filter($matches[0], function($line) {
// Filter references to @see as they don't really matter.
return strpos($line, '@see') === FALSE;
});
$this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path");
}
/**
......
......@@ -75,6 +75,7 @@ public function testReadOnly() {
// Saving and deleting should always fail.
$this->assertFalse($php_read->save($name, $code));
$this->assertFalse($php_read->delete($name));
unset($GLOBALS[$random]);
}
/**
......
......@@ -86,6 +86,7 @@ public function testDeleteAll() {
// Should still return TRUE if directory has already been deleted.
$this->assertTrue($php->deleteAll(), 'Delete all succeeds with nothing to delete');
unset($GLOBALS[$random]);
}
}
......@@ -124,6 +124,7 @@ public function testSecurity() {
$this->assertSame($php->load($name), $this->expected[$i]);
$this->assertSame($GLOBALS['hacked'], $this->expected[$i]);
}
unset($GLOBALS['hacked']);
}
}
......@@ -60,6 +60,7 @@ public function assertCRUD($php) {
// Ensure delete() can be called on a non-existing file. It should return
// FALSE, but not trigger errors.
$this->assertFalse($php->delete($name), 'Delete fails on missing file');
unset($GLOBALS[$random]);
}
}
......@@ -22,6 +22,7 @@ class ComposerIntegrationTest extends UnitTestCase {
*/
protected function getErrorMessages() {
$messages = [
0 => 'No errors found',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
......@@ -63,9 +64,7 @@ public function testComposerJson() {
$json = file_get_contents($path . '/composer.json');
$result = json_decode($json);
if (is_null($result)) {
$this->fail($this->getErrorMessages()[json_last_error()]);
}
$this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
}
}
......
......@@ -302,7 +302,7 @@ public function providerTestContextBubblingEdgeCases() {
* Tests the self-healing of the redirect with conditional cache contexts.
*/
public function testConditionalCacheContextBubblingSelfHealing() {
global $current_user_role;
$current_user_role = &$this->currentUserRole;
$this->setUpRequest();
$this->setupMemoryCache();
......@@ -319,8 +319,7 @@ public function testConditionalCacheContextBubblingSelfHealing() {
'tags' => ['b'],
],
'grandchild' => [
'#access_callback' => function () {
global $current_user_role;
'#access_callback' => function() use (&$current_user_role) {
// Only role A cannot access this subtree.
return $current_user_role !== 'A';
},
......@@ -331,8 +330,7 @@ public function testConditionalCacheContextBubblingSelfHealing() {
'max-age' => 1800,
],
'grandgrandchild' => [
'#access_callback' => function () {
global $current_user_role;
'#access_callback' => function () use (&$current_user_role) {
// Only role C can access this subtree.
return $current_user_role === 'C';
},
......
......@@ -599,7 +599,9 @@ public function testScalarLazybuilderCallbackContext() {
'null' => NULL,
]];
$this->renderer->renderRoot($element);
$result = $this->renderer->renderRoot($element);
$this->assertInstanceOf('\Drupal\Core\Render\SafeString', $result);
$this->assertEquals('<p>This is a rendered placeholder!</p>', (string) $result);
}
/**
......
......@@ -79,6 +79,13 @@ class RendererTestBase extends UnitTestCase {
*/
protected $memoryCache;
/**
* The simulated "current" user role, for use in tests with cache contexts.
*
* @var string
*/
protected $currentUserRole;
/**
* The mocked renderer configuration.
*
......@@ -113,10 +120,10 @@ protected function setUp() {
$this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$current_user_role = &$this->currentUserRole;
$this->cacheContextsManager->expects($this->any())
->method('convertTokensToKeys')
->willReturnCallback(function($context_tokens) {
global $current_user_role;
->willReturnCallback(function($context_tokens) use (&$current_user_role) {
$keys = [];
foreach ($context_tokens as $context_id) {
switch ($context_id) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment