Commit 523b60b5 authored by alexpott's avatar alexpott

Issue #2463419 by Mile23, rpayanm, hussainweb: Clean-up Test members in...

Issue #2463419 by Mile23, rpayanm, hussainweb: Clean-up Test members in core/tests - ensure property definition and use of camelCase naming convention
parent f7740e4d
......@@ -16,12 +16,6 @@
*/
class DiscoveryCachedTraitTest extends UnitTestCase {
// Temporary storage to mock a side-effect.
protected $trait;
protected $definitions_ref;
protected $get_definitions;
/**
* Data provider for testGetDefinition().
*
......@@ -46,25 +40,25 @@ public function providerGetDefinition() {
public function testGetDefinition($expected, $cached_definitions, $get_definitions, $plugin_id) {
// Mock a DiscoveryCachedTrait.
$trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait');
$definitions_ref = new \ReflectionProperty($trait, 'definitions');
$definitions_ref->setAccessible(TRUE);
$reflection_definitions = new \ReflectionProperty($trait, 'definitions');
$reflection_definitions->setAccessible(TRUE);
// getDefinition() needs the ::$definitions property to be set in one of two
// ways: 1) As existing cached data, or 2) as a side-effect of calling
// getDefinitions().
// If there are no cached definitions, then we have to fake the side-effect
// of getDefinitions().
if (count($cached_definitions) < 1) {
$this->trait = $trait;
$this->definitions_ref = $definitions_ref;
$this->get_definitions = $get_definitions;
// Use a callback method, so we can perform the side-effects.
$trait->expects($this->once())
->method('getDefinitions')
->willReturnCallback(array($this, 'getDefinitionsCallback'));
// Use a callback method, so we can perform the side-effects.
->willReturnCallback(function() use ($reflection_definitions, $trait, $get_definitions) {
$reflection_definitions->setValue($trait, $get_definitions);
return $get_definitions;
});
}
else {
// Put $cached_definitions into our mocked ::$definitions.
$definitions_ref->setValue($trait, $cached_definitions);
$reflection_definitions->setValue($trait, $cached_definitions);
}
// Call getDefinition(), with $exception_on_invalid always FALSE.
$this->assertSame(
......@@ -73,17 +67,4 @@ public function testGetDefinition($expected, $cached_definitions, $get_definitio
);
}
/**
* Callback method so we can mock the side-effects of getDefinitions().
*
* @see testGetDefinition
*/
public function getDefinitionsCallback() {
$this->definitions_ref->setValue(
$this->trait,
$this->get_definitions
);
return $this->get_definitions;
}
}
......@@ -54,7 +54,7 @@ class CssCollectionRendererUnitTest extends UnitTestCase {
*
* @var array
*/
protected $file_css_group;
protected $fileCssGroup;
/**
* The state mock class.
......@@ -69,7 +69,7 @@ protected function setUp() {
$this->state = $this->getMock('Drupal\Core\State\StateInterface');
$this->renderer = new CssCollectionRenderer($this->state);
$this->file_css_group = array(
$this->fileCssGroup = array(
'group' => -100,
'every_page' => TRUE,
'type' => 'file',
......
......@@ -55,20 +55,6 @@ class CssOptimizerUnitTest extends UnitTestCase {
*/
protected $optimizer;
/**
* A valid file CSS asset group.
*
* @var array
*/
protected $file_css_group;
/**
* A valid inline CSS asset group.
*
* @var array
*/
protected $inline_css_group;
protected function setUp() {
parent::setUp();
......
......@@ -16,12 +16,19 @@
*/
class PostgresqlConnectionTest extends UnitTestCase {
/**
* Mock PDO object for use in tests.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Tests\Core\Database\Stub\StubPDO
*/
protected $mockPdo;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
$this->mockPdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
}
/**
......@@ -84,7 +91,7 @@ public function providerEscapeFields() {
* @dataProvider providerEscapeTables
*/
public function testEscapeTable($expected, $name) {
$pgsql_connection = new Connection($this->mock_pdo, array());
$pgsql_connection = new Connection($this->mockPdo, []);
$this->assertEquals($expected, $pgsql_connection->escapeTable($name));
}
......@@ -94,7 +101,7 @@ public function testEscapeTable($expected, $name) {
* @dataProvider providerEscapeAlias
*/
public function testEscapeAlias($expected, $name) {
$pgsql_connection = new Connection($this->mock_pdo, array());
$pgsql_connection = new Connection($this->mockPdo, []);
$this->assertEquals($expected, $pgsql_connection->escapeAlias($name));
}
......@@ -104,7 +111,7 @@ public function testEscapeAlias($expected, $name) {
* @dataProvider providerEscapeFields
*/
public function testEscapeField($expected, $name) {
$pgsql_connection = new Connection($this->mock_pdo, array());
$pgsql_connection = new Connection($this->mockPdo, []);
$this->assertEquals($expected, $pgsql_connection->escapeField($name));
}
......
......@@ -127,6 +127,13 @@ class EntityManagerTest extends UnitTestCase {
*/
protected $eventDispatcher;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
......
......@@ -87,7 +87,7 @@ class ThemeHandlerTest extends UnitTestCase {
/**
* The tested theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandler|\Drupal\Tests\Core\Extension\TestThemeHandler
* @var \Drupal\Core\Extension\ThemeHandler|\Drupal\Tests\Core\Extension\StubThemeHandler
*/
protected $themeHandler;
......@@ -115,11 +115,11 @@ protected function setUp() {
$this->extensionDiscovery = $this->getMockBuilder('Drupal\Core\Extension\ExtensionDiscovery')
->disableOriginalConstructor()
->getMock();
$this->cssCollectionOptimizer = $this->getMockBuilder('\Drupal\Core\Asset\CssCollectionOptimizer') //\Drupal\Core\Asset\AssetCollectionOptimizerInterface');
$this->cssCollectionOptimizer = $this->getMockBuilder('\Drupal\Core\Asset\CssCollectionOptimizer')
->disableOriginalConstructor()
->getMock();
$logger = $this->getMock('Psr\Log\LoggerInterface');
$this->themeHandler = new TestThemeHandler($this->root, $this->configFactory, $this->moduleHandler, $this->state, $this->infoParser, $logger, $this->cssCollectionOptimizer, $this->configInstaller, $this->configManager, $this->routeBuilder, $this->extensionDiscovery);
$this->themeHandler = new StubThemeHandler($this->root, $this->configFactory, $this->moduleHandler, $this->state, $this->infoParser, $logger, $this->cssCollectionOptimizer, $this->configInstaller, $this->configManager, $this->routeBuilder, $this->extensionDiscovery);
$cache_tags_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$this->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
......@@ -330,7 +330,28 @@ public function providerTestGetBaseThemes() {
/**
* Extends the default theme handler to mock some drupal_ methods.
*/
class TestThemeHandler extends ThemeHandler {
class StubThemeHandler extends ThemeHandler {
/**
* Whether the CSS cache was cleared.
*
* @var bool
*/
protected $clearedCssCache;
/**
* Whether the registry should be rebuilt.
*
* @var bool
*/
protected $registryRebuild;
/**
* A list of themes keyed by name.
*
* @var array
*/
protected $systemList;
/**
* {@inheritdoc}
......
......@@ -16,13 +16,6 @@
*/
class LogMessageParserTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->parser = new LogMessageParser();
}
/**
* Test for LogMessageParserTrait::parseMessagePlaceholders()
*
......@@ -39,7 +32,8 @@ protected function setUp() {
* @covers ::parseMessagePlaceholders
*/
public function testParseMessagePlaceholders(array $value, array $expected) {
$message_placeholders = $this->parser->parseMessagePlaceholders($value['message'], $value['context']);
$parser = new LogMessageParser();
$message_placeholders = $parser->parseMessagePlaceholders($value['message'], $value['context']);
$this->assertEquals($expected['message'], $value['message']);
$this->assertEquals($expected['context'], $message_placeholders);
}
......
......@@ -67,6 +67,13 @@ class ContextualLinkManagerTest extends UnitTestCase {
*/
protected $accessManager;
/**
* The mocked account.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account;
protected function setUp() {
$this->contextualLinkManager = $this
->getMockBuilder('Drupal\Core\Menu\ContextualLinkManager')
......
......@@ -51,7 +51,7 @@ class RouteBuilderTest extends UnitTestCase {
protected $dispatcher;
/**
* The mocked yaml discovery.
* The mocked YAML discovery.
*
* @var \Drupal\Component\Discovery\YamlDiscovery|\PHPUnit_Framework_MockObject_MockObject
*/
......@@ -276,15 +276,22 @@ public function testRebuildIfNeeded() {
}
/**
* Extends the core route builder with a setter method for the yaml discovery.
* Extends the core route builder with a setter method for the YAML discovery.
*/
class TestRouteBuilder extends RouteBuilder {
/**
* Sets the yaml discovery.
* The mocked YAML discovery.
*
* @var \Drupal\Component\Discovery\YamlDiscovery|\PHPUnit_Framework_MockObject_MockObject
*/
protected $yamlDiscovery;
/**
* Sets the YAML discovery.
*
* @param \Drupal\Component\Discovery\YamlDiscovery $yaml_discovery
* The yaml discovery to set.
* The YAML discovery to set.
*/
public function setYamlDiscovery(YamlDiscovery $yaml_discovery) {
$this->yamlDiscovery = $yaml_discovery;
......
......@@ -24,28 +24,28 @@ class PermissionsHashTest extends UnitTestCase {
*
* @var \Drupal\user\UserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account_1;
protected $account1;
/**
* An "updated" mocked account.
*
* @var \Drupal\user\UserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account_1_updated;
protected $account1Updated;
/**
* A different account.
*
* @var \Drupal\user\UserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account_2;
protected $account2;
/**
* The mocked private key service.
*
* @var \Drupal\Core\PrivateKey|\PHPUnit_Framework_MockObject_MockObject
*/
protected $private_key;
protected $privateKey;
/**
* The mocked cache backend.
......@@ -71,48 +71,48 @@ protected function setUp() {
// Account 1: 'administrator' and 'authenticated' roles.
$roles_1 = array('administrator', 'authenticated');
$this->account_1 = $this->getMockBuilder('Drupal\user\Entity\User')
$this->account1 = $this->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->setMethods(array('getRoles'))
->getMock();
$this->account_1->expects($this->any())
$this->account1->expects($this->any())
->method('getRoles')
->will($this->returnValue($roles_1));
// Account 2: 'authenticated' and 'administrator' roles (different order).
$roles_2 = array('authenticated', 'administrator');
$this->account_2 = $this->getMockBuilder('Drupal\user\Entity\User')
$this->account2 = $this->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->setMethods(array('getRoles'))
->getMock();
$this->account_2->expects($this->any())
$this->account2->expects($this->any())
->method('getRoles')
->will($this->returnValue($roles_2));
// Updated account 1: now also 'editor' role.
$roles_1_updated = array('editor', 'administrator', 'authenticated');
$this->account_1_updated = $this->getMockBuilder('Drupal\user\Entity\User')
$this->account1Updated = $this->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->setMethods(array('getRoles'))
->getMock();
$this->account_1_updated->expects($this->any())
$this->account1Updated->expects($this->any())
->method('getRoles')
->will($this->returnValue($roles_1_updated));
// Mocked private key + cache services.
$random = Crypt::randomBytesBase64(55);
$this->private_key = $this->getMockBuilder('Drupal\Core\PrivateKey')
$this->privateKey = $this->getMockBuilder('Drupal\Core\PrivateKey')
->disableOriginalConstructor()
->setMethods(array('get'))
->getMock();
$this->private_key->expects($this->any())
$this->privateKey->expects($this->any())
->method('get')
->will($this->returnValue($random));
$this->cache = $this->getMockBuilder('Drupal\Core\Cache\CacheBackendInterface')
->disableOriginalConstructor()
->getMock();
$this->permissionsHash = new PermissionsHashGenerator($this->private_key, $this->cache);
$this->permissionsHash = new PermissionsHashGenerator($this->privateKey, $this->cache);
}
/**
......@@ -120,12 +120,12 @@ protected function setUp() {
*/
public function testGenerate() {
// Ensure that two user accounts with the same roles generate the same hash.
$hash_1 = $this->permissionsHash->generate($this->account_1);
$hash_2 = $this->permissionsHash->generate($this->account_2);
$hash_1 = $this->permissionsHash->generate($this->account1);
$hash_2 = $this->permissionsHash->generate($this->account2);
$this->assertSame($hash_1, $hash_2, 'Different users with the same roles generate the same permissions hash.');
// Compare with hash for user account 1 with an additional role.
$updated_hash_1 = $this->permissionsHash->generate($this->account_1_updated);
$updated_hash_1 = $this->permissionsHash->generate($this->account1Updated);
$this->assertNotSame($hash_1, $updated_hash_1, 'Same user with updated roles generates different permissions hash.');
}
......@@ -146,7 +146,7 @@ public function testGenerateCache() {
$this->cache->expects($this->never())
->method('set');
$this->permissionsHash->generate($this->account_1);
$this->permissionsHash->generate($this->account1);
}
/**
......@@ -164,7 +164,7 @@ public function testGenerateNoCache() {
->method('set')
->with($expected_cid, $this->isType('string'));
$this->permissionsHash->generate($this->account_1);
$this->permissionsHash->generate($this->account1);
}
}
......
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