Skip to content

Commit

Permalink
Issue #3131223 by jungle, mondrake, longwave: Replace assertions invo…
Browse files Browse the repository at this point in the history
…lving calls to array_key_exists() with assertArrayHasKey()/assertArrayNotHasKey()
  • Loading branch information
catch committed Apr 28, 2020
1 parent cbfb75a commit 54158e0
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function testFormatterUI() {
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
$display = $display_storage->loadUnchanged($display_id);
$component = $display->getComponent('field_test');
$this->assertFalse(array_key_exists('field_third_party_test', $component['third_party_settings']));
$this->assertArrayNotHasKey('field_third_party_test', $component['third_party_settings']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testNumericStyleName() {
$this->drupalPostForm('admin/config/media/image-styles/add', $edit, t('Create new style'));
$this->assertRaw(t('Style %name was created.', ['%name' => $style_label]));
$options = image_style_options();
$this->assertTrue(array_key_exists($style_name, $options), new FormattableMarkup('Array key %key exists.', ['%key' => $style_name]));
$this->assertArrayHasKey($style_name, $options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ public function testContentCustomization() {
$this->assertTrue(in_array('language_content', $config->get('configurable')), 'Content language is configurable.');

// Ensure configuration was saved.
$this->assertFalse(array_key_exists('language-url', $config->get('negotiation.language_content.enabled')), 'URL negotiation is not enabled for content.');
$this->assertTrue(array_key_exists('language-session', $config->get('negotiation.language_content.enabled')), 'Session negotiation is enabled for content.');
$this->assertArrayNotHasKey('language-url', $config->get('negotiation.language_content.enabled'));
$this->assertArrayHasKey('language-session', $config->get('negotiation.language_content.enabled'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ public function testNodeAccessLanguageAwareCombination() {
// Four nodes should be returned with public Hungarian translations or the
// no language public node.
$this->assertEqual(count($nids), 4, 'Query returns 4 nodes when no langcode is specified.');
$this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is full public node.');
$this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.');
$this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$this->assertTrue(array_key_exists($this->nodes['public_no_language_public']->id(), $nids), 'Returned node ID is no language public node.');
$this->assertArrayHasKey($this->nodes['public_both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['public_ca_private']->id(), $nids);
$this->assertArrayHasKey($this->nodes['private_both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['public_no_language_public']->id(), $nids);

// Query with Hungarian (hu) specified.
$select = $connection->select('node', 'n')
Expand All @@ -278,9 +278,9 @@ public function testNodeAccessLanguageAwareCombination() {

// Three nodes should be returned (with public Hungarian translations).
$this->assertEqual(count($nids), 3, 'Query returns 3 nodes.');
$this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.');
$this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.');
$this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$this->assertArrayHasKey($this->nodes['public_both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['public_ca_private']->id(), $nids);
$this->assertArrayHasKey($this->nodes['private_both_public']->id(), $nids);

// Query with Catalan (ca) specified.
$select = $connection->select('node', 'n')
Expand All @@ -292,9 +292,9 @@ public function testNodeAccessLanguageAwareCombination() {

// Three nodes should be returned (with public Catalan translations).
$this->assertEqual(count($nids), 3, 'Query returns 3 nodes.');
$this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.');
$this->assertTrue(array_key_exists($this->nodes['public_hu_private']->id(), $nids), 'Returned node ID is Catalan public only node.');
$this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.');
$this->assertArrayHasKey($this->nodes['public_both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['public_hu_private']->id(), $nids);
$this->assertArrayHasKey($this->nodes['private_both_public']->id(), $nids);

// Query with German (de) specified.
$select = $connection->select('node', 'n')
Expand Down
14 changes: 7 additions & 7 deletions modules/node/tests/src/Kernel/NodeAccessLanguageAwareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public function testNodeAccessLanguageAware() {
// - Node with only the Catalan translation marked as private.
// - No language node marked as public.
$this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes when no langcode is specified.');
$this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
$this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.');
$this->assertTrue(array_key_exists($this->nodes['no_language_public']->id(), $nids), 'The node with no language is returned.');
$this->assertArrayHasKey($this->nodes['both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['ca_private']->id(), $nids);
$this->assertArrayHasKey($this->nodes['no_language_public']->id(), $nids);

// Query with Hungarian (hu) specified.
$select = $connection->select('node', 'n')
Expand All @@ -221,8 +221,8 @@ public function testNodeAccessLanguageAware() {
// Two nodes should be returned: the node with both translations public, and
// the node with only the Catalan translation marked as private.
$this->assertEqual(count($nids), 2, 'Query returns 2 nodes when the hu langcode is specified.');
$this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
$this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.');
$this->assertArrayHasKey($this->nodes['both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['ca_private']->id(), $nids);

// Query with Catalan (ca) specified.
$select = $connection->select('node', 'n')
Expand All @@ -235,8 +235,8 @@ public function testNodeAccessLanguageAware() {
// Two nodes should be returned: the node with both translations public, and
// the node with only the Hungarian translation marked as private.
$this->assertEqual(count($nids), 2, 'Query returns 2 nodes when the hu langcode is specified.');
$this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.');
$this->assertTrue(array_key_exists($this->nodes['hu_private']->id(), $nids), 'The node with only the Hungarian translation private is returned.');
$this->assertArrayHasKey($this->nodes['both_public']->id(), $nids);
$this->assertArrayHasKey($this->nodes['hu_private']->id(), $nids);

// Query with German (de) specified.
$select = $connection->select('node', 'n')
Expand Down
4 changes: 2 additions & 2 deletions modules/node/tests/src/Kernel/NodeAccessLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public function testNodeAccessQueryTag() {
// The public node and no language node should be returned. Because no
// langcode is given it will use the fallback node.
$this->assertEqual(count($nids), 2, 'Query returns 2 node');
$this->assertTrue(array_key_exists($node_public->id(), $nids), 'Returned node ID is public node.');
$this->assertTrue(array_key_exists($node_no_language->id(), $nids), 'Returned node ID is no language node.');
$this->assertArrayHasKey($node_public->id(), $nids);
$this->assertArrayHasKey($node_no_language->id(), $nids);

// Query the nodes table as the web user with the node access tag and
// langcode de.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public function testUserNormalize() {
// Test password isn't available.
$normalized = $this->serializer->normalize($this->user);

$this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
$this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
$this->assertArrayNotHasKey('pass', $normalized);
$this->assertArrayNotHasKey('mail', $normalized);

// Test again using our test user, so that our access control override will
// allow password viewing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function testUpdateNewDependency() {
// Running the updates enables the dependency.
$this->runUpdates();

$this->assertTrue(array_key_exists('new_dependency_test', $this->container->get('config.factory')->get('core.extension')->get('module')));
$this->assertTrue(array_key_exists('new_dependency_test_with_service', $this->container->get('config.factory')->get('core.extension')->get('module')));
$this->assertArrayHasKey('new_dependency_test', $this->container->get('config.factory')->get('core.extension')->get('module'));
$this->assertArrayHasKey('new_dependency_test_with_service', $this->container->get('config.factory')->get('core.extension')->get('module'));

// Tests that the new services are available and working as expected.
$this->assertEquals('Hello', $this->container->get('new_dependency_test_with_service.service')->greet());
Expand Down
6 changes: 3 additions & 3 deletions modules/views/tests/src/Kernel/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ public function testLoadFunctions() {
$this->assertIdentical(array_keys($all_views_sorted), array_keys(Views::getViewsAsOptions(TRUE, 'all', NULL, FALSE, TRUE)), 'All view id keys returned in expected sort order');

// Test $exclude_view parameter.
$this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive')), 'View excluded from options based on name');
$this->assertFalse(array_key_exists('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default')), 'View display excluded from options based on name');
$this->assertFalse(array_key_exists('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable())), 'View excluded from options based on object');
$this->assertArrayNotHasKey('archive', Views::getViewsAsOptions(TRUE, 'all', 'archive'));
$this->assertArrayNotHasKey('archive:default', Views::getViewsAsOptions(FALSE, 'all', 'archive:default'));
$this->assertArrayNotHasKey('archive', Views::getViewsAsOptions(TRUE, 'all', $archive->getExecutable()));

// Test the $opt_group parameter.
$expected_opt_groups = [];
Expand Down
2 changes: 1 addition & 1 deletion modules/views/tests/src/Kernel/PluginInstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testPluginData() {

// Check all plugin types.
foreach ($this->pluginTypes as $type) {
$this->assertTrue(array_key_exists($type, $this->definitions), new FormattableMarkup('Key for plugin type @type found.', ['@type' => $type]));
$this->assertArrayHasKey($type, $this->definitions);
$this->assertTrue(is_array($this->definitions[$type]) && !empty($this->definitions[$type]), new FormattableMarkup('Plugin type @type has an array of plugins.', ['@type' => $type]));
}

Expand Down
2 changes: 1 addition & 1 deletion modules/views_ui/tests/src/Functional/ViewEditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testOtherOptions() {
$displays = $view->get('display');
$this->assertTrue(!empty($displays['test_1']), 'Display data found for new display ID key.');
$this->assertIdentical($displays['test_1']['id'], 'test_1', 'New display ID matches the display ID key.');
$this->assertFalse(array_key_exists('attachment_1', $displays), 'Old display ID not found.');
$this->assertArrayNotHasKey('attachment_1', $displays);

// Set to the same machine name and save the View.
$edit = ['display_id' => 'test_1'];
Expand Down
16 changes: 8 additions & 8 deletions tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function testAddFiles() {

$css = $this->assetResolver->getCssAssets($assets, FALSE);
$js = $this->assetResolver->getJsAssets($assets, FALSE)[1];
$this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/bar.css', $css), 'CSS files are correctly added.');
$this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/foo.js', $js), 'JavaScript files are correctly added.');
$this->assertArrayHasKey('core/modules/system/tests/modules/common_test/bar.css', $css);
$this->assertArrayHasKey('core/modules/system/tests/modules/common_test/foo.js', $js);

$css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css);
$js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
Expand All @@ -103,8 +103,8 @@ public function testAddJsSettings() {

$this->assertEqual([], $assets->getSettings(), 'JavaScript settings on $assets are empty.');
$javascript = $this->assetResolver->getJsAssets($assets, FALSE)[1];
$this->assertTrue(array_key_exists('currentPath', $javascript['drupalSettings']['data']['path']), 'The current path JavaScript setting is set correctly.');
$this->assertTrue(array_key_exists('currentPath', $assets->getSettings()['path']), 'JavaScript settings on $assets are resolved after retrieving JavaScript assets, and are equal to the returned JavaScript settings.');
$this->assertArrayHasKey('currentPath', $javascript['drupalSettings']['data']['path']);
$this->assertArrayHasKey('currentPath', $assets->getSettings()['path']);

$assets->setSettings(['drupal' => 'rocks', 'dries' => 280342800]);
$javascript = $this->assetResolver->getJsAssets($assets, FALSE)[1];
Expand All @@ -121,8 +121,8 @@ public function testAddExternalFiles() {

$css = $this->assetResolver->getCssAssets($assets, FALSE);
$js = $this->assetResolver->getJsAssets($assets, FALSE)[1];
$this->assertTrue(array_key_exists('http://example.com/stylesheet.css', $css), 'External CSS files are correctly added.');
$this->assertTrue(array_key_exists('http://example.com/script.js', $js), 'External JavaScript files are correctly added.');
$this->assertArrayHasKey('http://example.com/stylesheet.css', $css);
$this->assertArrayHasKey('http://example.com/script.js', $js);

$css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css);
$js_render_array = \Drupal::service('asset.js.collection_renderer')->render($js);
Expand Down Expand Up @@ -464,8 +464,8 @@ public function testAddJsFileWithQueryString() {

$css = $this->assetResolver->getCssAssets($assets, FALSE);
$js = $this->assetResolver->getJsAssets($assets, FALSE)[1];
$this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2', $css), 'CSS file with query string is correctly added.');
$this->assertTrue(array_key_exists('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js), 'JavaScript file with query string is correctly added.');
$this->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.css?arg1=value1&arg2=value2', $css);
$this->assertArrayHasKey('core/modules/system/tests/modules/common_test/querystring.js?arg1=value1&arg2=value2', $js);

$css_render_array = \Drupal::service('asset.css.collection_renderer')->render($css);
$rendered_css = $this->renderer->renderPlain($css_render_array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function testNoTemplate() {
$entity = $this->createTestEntity('entity_test');
$build = $entity_type_manager->getViewBuilder('entity_test')->view($entity);
$this->assertEquals($entity, $build['#entity_test']);
$this->assertFalse(array_key_exists('#theme', $build));
$this->assertArrayNotHasKey('#theme', $build);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function testModuleImplementsAlter() {
$this->assertTrue(function_exists('module_test_modules_installed'),
'The file module_test.module was successfully included.');

$this->assertTrue(array_key_exists('module_test', \Drupal::moduleHandler()->getModuleList()),
'module_test is in the module list.');
$this->assertArrayHasKey('module_test', \Drupal::moduleHandler()->getModuleList());

$this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('modules_installed')),
'module_test implements hook_modules_installed().');
Expand Down
14 changes: 7 additions & 7 deletions tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ public function testCandidateOutlines() {
$candidates = array_flip($candidates);

$this->assertCount(7, $candidates, 'Correct number of candidates found');
$this->assertTrue(array_key_exists('/node/5/edit', $candidates), 'First candidate found.');
$this->assertTrue(array_key_exists('/node/5/%', $candidates), 'Second candidate found.');
$this->assertTrue(array_key_exists('/node/%/edit', $candidates), 'Third candidate found.');
$this->assertTrue(array_key_exists('/node/%/%', $candidates), 'Fourth candidate found.');
$this->assertTrue(array_key_exists('/node/5', $candidates), 'Fifth candidate found.');
$this->assertTrue(array_key_exists('/node/%', $candidates), 'Sixth candidate found.');
$this->assertTrue(array_key_exists('/node', $candidates), 'Seventh candidate found.');
$this->assertArrayHasKey('/node/5/edit', $candidates);
$this->assertArrayHasKey('/node/5/%', $candidates);
$this->assertArrayHasKey('/node/%/edit', $candidates);
$this->assertArrayHasKey('/node/%/%', $candidates);
$this->assertArrayHasKey('/node/5', $candidates);
$this->assertArrayHasKey('/node/%', $candidates);
$this->assertArrayHasKey('/node', $candidates);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function testVersion() {

$libraries = $this->libraryDiscoveryParser->buildByExtension('versions');

$this->assertFalse(array_key_exists('version', $libraries['versionless']));
$this->assertArrayNotHasKey('version', $libraries['versionless']);
$this->assertEquals(-1, $libraries['versionless']['css'][0]['version']);
$this->assertEquals(-1, $libraries['versionless']['js'][0]['version']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testGetMultiple() {
$this->assertSame($ret['t123']->data, 1231, 'Got key 123 and value is from the first backend');
$this->assertSame($ret['t23']->data, 232, 'Got key 23 and value is from the second backend');
$this->assertSame($ret['t3']->data, 33, 'Got key 3 and value is from the third backend');
$this->assertFalse(array_key_exists('t4', $ret), "Didn't get the nonexistent key");
$this->assertArrayNotHasKey('t4', $ret);

$this->assertFalse(in_array('t123', $cids), "Existing key 123 has been removed from &\$cids");
$this->assertFalse(in_array('t23', $cids), "Existing key 23 has been removed from &\$cids");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function testCheckAccess() {
$this->assertInstanceOf('\Drupal\Core\Menu\InaccessibleMenuLink', $element->link);
// Menu link 4: child of menu link 3, which was AccessResult::neutral(),
// hence menu link 3's subtree is removed, of which this menu link is one.
$this->assertFalse(array_key_exists(4, $tree[2]->subtree[3]->subtree));
$this->assertArrayNotHasKey(4, $tree[2]->subtree[3]->subtree);
// Menu link 5: no route name, treated as external, hence access granted.
$element = $tree[5];
$this->assertEquals(AccessResult::allowed()->cachePerPermissions(), $element->access);
Expand Down

0 comments on commit 54158e0

Please sign in to comment.