Skip to content

Commit

Permalink
Issue #3128814 by longwave, jungle, dww, alexpott: Replace assert* in…
Browse files Browse the repository at this point in the history
…volving count() and an equality operator with assertCount()
  • Loading branch information
catch committed Apr 28, 2020
1 parent 187e92e commit 6d59517
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 88 deletions.
8 changes: 4 additions & 4 deletions modules/ckeditor/tests/src/Functional/CKEditorAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testExistingFormat() {
// Ensure the styles textarea exists and is initialized empty.
$styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
$this->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
$this->assertTrue(count($styles_textarea) === 1, 'The "styles" textarea exists.');
$this->assertCount(1, $styles_textarea, 'The "styles" textarea exists.');

// Submit the form to save the selection of CKEditor as the chosen editor.
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
Expand Down Expand Up @@ -203,7 +203,7 @@ public function testExistingFormat() {
$this->container->get('plugin.manager.ckeditor.plugin')->clearCachedDefinitions();
$this->drupalGet('admin/config/content/formats/manage/filtered_html');
$ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and not(@checked)]');
$this->assertTrue(count($ultra_llama_mode_checkbox) === 1, 'The "Ultra llama mode" checkbox exists and is not checked.');
$this->assertCount(1, $ultra_llama_mode_checkbox, 'The "Ultra llama mode" checkbox exists and is not checked.');
$editor = Editor::load('filtered_html');
$this->assertInstanceOf(Editor::class, $editor);
$this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
Expand All @@ -216,7 +216,7 @@ public function testExistingFormat() {
$this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->drupalGet('admin/config/content/formats/manage/filtered_html');
$ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and @checked="checked"]');
$this->assertTrue(count($ultra_llama_mode_checkbox) === 1, 'The "Ultra llama mode" checkbox exists and is checked.');
$this->assertCount(1, $ultra_llama_mode_checkbox, 'The "Ultra llama mode" checkbox exists and is checked.');
$expected_settings['plugins']['llama_contextual_and_button']['ultra_llama_mode'] = TRUE;
$editor = Editor::load('filtered_html');
$this->assertInstanceOf(Editor::class, $editor);
Expand Down Expand Up @@ -285,7 +285,7 @@ public function testNewFormat() {
// Ensure the styles textarea exists and is initialized empty.
$styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
$this->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
$this->assertTrue(count($styles_textarea) === 1, 'The "styles" textarea exists.');
$this->assertCount(1, $styles_textarea, 'The "styles" textarea exists.');

// Submit the form to create both a new text format and an associated text
// editor.
Expand Down
12 changes: 6 additions & 6 deletions modules/ckeditor/tests/src/Functional/CKEditorLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public function testLoading() {
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
$this->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$hidden_input = $this->xpath('//input[@type="hidden" and contains(@class, "editor")]');
$this->assertTrue(count($hidden_input) === 0, 'A single text format hidden input does not exist on the page.');
$this->assertCount(0, $hidden_input, 'A single text format hidden input does not exist on the page.');
$this->assertNoRaw(drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js', 'CKEditor glue JS is absent.');

// On pages where there would never be a text editor, CKEditor JS is absent.
Expand Down Expand Up @@ -120,10 +120,10 @@ public function testLoading() {
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertIdentical($expected, $this->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
$this->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
$this->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.');

// Enable the ckeditor_test module, customize configuration. In this case,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testImport() {
// Ensure submit button for \Drupal\config\Form\ConfigImportForm is
// disabled.
$submit_is_disabled = $this->cssSelect('form.config-import-form input[type="submit"]:disabled');
$this->assertTrue(count($submit_is_disabled) === 1, 'The submit button is disabled.');
$this->assertCount(1, $submit_is_disabled, 'The submit button is disabled.');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ protected function doTestTranslationDeletion() {
$entity = $storage->load($this->entityId, TRUE);
if ($this->assertTrue(is_object($entity), 'Entity found')) {
$translations = $entity->getTranslationLanguages();
$this->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
$this->assertCount(2, $translations, 'Translation successfully deleted.');
$this->assertEmpty($translations[$langcode], 'Translation successfully deleted.');
}

// Check that the translator cannot delete the original translation.
Expand Down
20 changes: 10 additions & 10 deletions modules/editor/tests/src/Functional/EditorAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public function testNoEditorAvailable() {
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
$this->assertTrue(count($select_is_disabled) === 1, 'The Text Editor select is disabled.');
$this->assertTrue(count($options) === 1, 'The Text Editor select has only one option.');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(1, $select_is_disabled, 'The Text Editor select is disabled.');
$this->assertCount(1, $options, 'The Text Editor select has only one option.');
$this->assertTrue(($options[0]->getText()) === 'None', 'Option 1 in the Text Editor select is "None".');
$this->assertRaw('This option is disabled because no modules that provide a text editor are currently enabled.', 'Description for select present that tells users to install a text editor module.');
}
Expand All @@ -94,7 +94,7 @@ public function testAddEditorToExistingFormat() {
];
$this->drupalPostForm(NULL, $edit, 'Configure');
$unicorn_setting = $this->xpath('//input[@name="editor[settings][ponies_too]" and @type="checkbox" and @checked]');
$this->assertTrue(count($unicorn_setting) === 0, "Unicorn Editor's settings form is no longer present.");
$this->assertCount(0, $unicorn_setting, "Unicorn Editor's settings form is no longer present.");
}

/**
Expand Down Expand Up @@ -196,9 +196,9 @@ protected function selectUnicornEditor() {
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
$this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
$this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$this->assertTrue(($options[0]->getText()) === 'None', 'Option 1 in the Text Editor select is "None".');
$this->assertTrue(($options[1]->getText()) === 'Unicorn Editor', 'Option 2 in the Text Editor select is "Unicorn Editor".');
$this->assertTrue($options[0]->hasAttribute('selected'), 'Option 1 ("None") is selected.');
Expand Down Expand Up @@ -233,9 +233,9 @@ protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TR
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertTrue(count($select) === 1, 'The Text Editor select exists.');
$this->assertTrue(count($select_is_disabled) === 0, 'The Text Editor select is not disabled.');
$this->assertTrue(count($options) === 2, 'The Text Editor select has two options.');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$this->assertTrue($options[1]->hasAttribute('selected'), 'Option 2 ("Unicorn Editor") is selected.');
}

Expand Down
34 changes: 17 additions & 17 deletions modules/editor/tests/src/Functional/EditorLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function testLoading() {
list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
$this->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page because the user only has access to a single format.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page because the user only has access to a single format.');
$this->drupalLogout($this->normalUser);

// The privileged user:
Expand All @@ -160,10 +160,10 @@ public function testLoading() {
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertIdentical($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
$this->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');

// Load the editor image dialog form and make sure it does not fatal.
$this->drupalGet('editor/dialog/image/full_html');
Expand Down Expand Up @@ -198,10 +198,10 @@ public function testLoading() {
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertIdentical($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$hidden_input = $this->xpath('//input[@type="hidden" and @value="plain_text" and @data-editor-for="edit-body-0-value"]');
$this->assertTrue(count($hidden_input) === 1, 'A single text format hidden input exists on the page and has a "data-editor-for" attribute with the correct value.');
$this->assertCount(1, $hidden_input, 'A single text format hidden input exists on the page and has a "data-editor-for" attribute with the correct value.');

// Create an "article" node that uses the full_html text format, then try
// to let the untrusted user edit it.
Expand All @@ -219,11 +219,11 @@ public function testLoading() {
list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
$this->assertTrue($editor_settings_present, 'Text Editor module settings.');
$this->assertTrue($editor_js_present, 'Text Editor JavaScript.');
$this->assertTrue(count($body) === 1, 'A body field exists.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertFieldByXPath('//textarea[@id="edit-body-0-value" and @disabled="disabled"]', t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
$this->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$hidden_input = $this->xpath('//input[@type="hidden" and contains(@class, "editor")]');
$this->assertTrue(count($hidden_input) === 0, 'A single text format hidden input does not exist on the page.');
$this->assertCount(0, $hidden_input, 'A single text format hidden input does not exist on the page.');
}

/**
Expand Down Expand Up @@ -258,10 +258,10 @@ public function testSupportedElementTypes() {
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this->getThingsToCheck('field-text', 'input');
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertTrue(count($field) === 1, 'A text field exists.');
$this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
$this->assertCount(1, $field, 'A text field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');

// Associate the trex text editor with the "Full HTML" text format.
$editor->delete();
Expand All @@ -274,10 +274,10 @@ public function testSupportedElementTypes() {
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this->getThingsToCheck('field-text', 'input');
$this->assertFalse($editor_settings_present, "Text Editor module's JavaScript settings are not on the page.");
$this->assertFalse($editor_js_present, 'Text Editor JavaScript is not present.');
$this->assertTrue(count($field) === 1, 'A text field exists.');
$this->assertTrue(count($format_selector) === 1, 'A single text format selector exists on the page.');
$this->assertCount(1, $field, 'A text field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this->assertFalse(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
$this->assertCount(0, $specific_format_selector, 'No text format selector exists on the page with the "editor" class and a "data-editor-for" attribute with the expected value.');
}

protected function getThingsToCheck($field_name, $type = 'textarea') {
Expand Down
6 changes: 4 additions & 2 deletions modules/field/tests/src/Kernel/FieldStorageCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,13 @@ public function testRead() {
// Check that 'single column' criteria works.
$field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name']]);
$this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.');
$this->assertCount(1, $fields, 'The field was properly read.');
$this->assertArrayHasKey($id, $fields, 'The field has the correct key.');

// Check that 'multi column' criteria works.
$fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'type' => $field_storage_definition['type']]);
$this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.');
$this->assertCount(1, $fields, 'The field was properly read.');
$this->assertArrayHasKey($id, $fields, 'The field has the correct key.');
$fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'type' => 'foo']);
$this->assertTrue(empty($fields), 'No field was found.');

Expand Down
2 changes: 1 addition & 1 deletion modules/field_ui/tests/src/Functional/FieldUIRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testFieldUIRoutes() {
$this->drupalGet('admin/config/people/accounts/form-display/register');
$this->assertTitle('Manage form display | Drupal');
$this->assertLocalTasks();
$this->assert(count($this->xpath('//ul/li[1]/a[contains(text(), :text)]', [':text' => 'Default'])) == 1, 'Default secondary tab is in first position.');
$this->assertCount(1, $this->xpath('//ul/li[1]/a[contains(text(), :text)]', [':text' => 'Default']), 'Default secondary tab is in first position.');

// Create new view mode and verify it's available on the Manage Display
// screen after enabling it.
Expand Down

0 comments on commit 6d59517

Please sign in to comment.