Commit d8348473 authored by catch's avatar catch
Browse files

Issue #3166450 by mondrake, longwave, snehalgaikwad: Split assertTrue using &&...

Issue #3166450 by mondrake, longwave, snehalgaikwad: Split assertTrue using && into multiple assertions

(cherry picked from commit 7dbdbcb1)
parent a5dac865
......@@ -57,12 +57,15 @@ protected function getMails(array $filter = []) {
* this default.
*
* @return bool
* TRUE on pass, FALSE on fail.
* TRUE on pass.
*/
protected function assertMail($name, $value = '', $message = '', $group = 'Email') {
$captured_emails = $this->container->get('state')->get('system.test_mail_collector') ?: [];
$email = end($captured_emails);
return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, $group);
$this->assertIsArray($email, $message);
$this->assertArrayHasKey($name, $email, $message);
$this->assertEquals($value, $email[$name], $message);
return TRUE;
}
/**
......
......@@ -269,7 +269,8 @@ public function testBigPipeNoJs() {
$this->assertNoRaw(BigPipe::STOP_SIGNAL);
// Verifying BigPipe assets are absent.
$this->assertTrue(!isset($this->getDrupalSettings()['bigPipePlaceholderIds']) && empty($this->getDrupalSettings()['ajaxPageState']), 'BigPipe drupalSettings and BigPipe asset library absent.');
$this->assertArrayNotHasKey('bigPipePlaceholderIds', $this->getDrupalSettings());
$this->assertArrayNotHasKey('ajaxPageState', $this->getDrupalSettings());
$this->assertRaw('</body>');
// Verify that 4xx responses work fine. (4xx responses are handled by
......
......@@ -207,7 +207,9 @@ public function testBlockViewBuilderViewAlter() {
$build = $this->getBlockRenderArray();
$this->assertFalse(isset($build['#prefix']), 'The appended #pre_render callback has not yet run before rendering.');
$this->assertIdentical((string) $this->renderer->renderRoot($build), 'Hiya!<br>');
$this->assertTrue(isset($build['#prefix']) && $build['#prefix'] === 'Hiya!<br>', 'A cached block without content is altered.');
// Check that a cached block without content is altered.
$this->assertArrayHasKey('#prefix', $build);
$this->assertSame('Hiya!<br>', $build['#prefix']);
}
/**
......
......@@ -40,14 +40,16 @@ public function testBlockMigration() {
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = BlockContent::load(1);
$this->assertIdentical('My block 1', $block->label());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
$this->assertGreaterThanOrEqual(REQUEST_TIME, $block->getChangedTime());
$this->assertLessThanOrEqual(time(), $block->getChangedTime());
$this->assertIdentical('en', $block->language()->getId());
$this->assertIdentical('<h3>My first custom block body</h3>', $block->body->value);
$this->assertIdentical('full_html', $block->body->format);
$block = BlockContent::load(2);
$this->assertIdentical('My block 2', $block->label());
$this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
$this->assertGreaterThanOrEqual(REQUEST_TIME, $block->getChangedTime());
$this->assertLessThanOrEqual(time(), $block->getChangedTime());
$this->assertIdentical('en', $block->language()->getId());
$this->assertIdentical('<h3>My second custom block body</h3>', $block->body->value);
$this->assertIdentical('full_html', $block->body->format);
......
......@@ -101,8 +101,8 @@ public function testCommentInterface() {
// Test changing the comment author to "Anonymous".
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => '']);
$this->assertTrue($comment->getAuthorName() == 'Anonymous', 'Comment author successfully changed to anonymous.');
$this->assertTrue($comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
$this->assertSame('Anonymous', $comment->getAuthorName());
$this->assertEquals(0, $comment->getOwnerId());
// Test changing the comment author to an unverified user.
$random_name = $this->randomMachineName();
......@@ -114,7 +114,8 @@ public function testCommentInterface() {
// Test changing the comment author to a verified user.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), ['uid' => $this->webUser->getAccountName() . ' (' . $this->webUser->id() . ')']);
$this->assertTrue($comment->getAuthorName() == $this->webUser->getAccountName() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
$this->assertSame($this->webUser->getAccountName(), $comment->getAuthorName());
$this->assertSame($this->webUser->id(), $comment->getOwnerId());
$this->drupalLogout();
......
......@@ -176,7 +176,8 @@ public function postComment(EntityInterface $entity, $comment, $subject = '', $c
$this->assertText($subject, 'Comment subject posted.');
}
$this->assertText($comment, 'Comment body posted.');
$this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.');
// Check the comment ID was extracted.
$this->assertArrayHasKey(1, $match);
}
if (isset($match[1])) {
......
......@@ -175,7 +175,8 @@ public function postComment($entity, $comment, $subject = '', $contact = NULL, $
$this->assertText($subject, 'Comment subject posted.');
}
$this->assertText($comment, 'Comment body posted.');
$this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
// Check the comment ID was extracted.
$this->assertArrayHasKey(1, $match);
}
if (isset($match[1])) {
......
......@@ -70,8 +70,7 @@ public function testInstallProfileConfigOverwrite() {
$config = $this->config('system.site');
// Verify the system.site config has a valid UUID.
$site_uuid = $config->get('uuid');
$this->assertTrue(Uuid::isValid($site_uuid) && strlen($site_uuid) > 0, "Site UUID '$site_uuid' is valid");
$this->assertTrue(Uuid::isValid($config->get('uuid')));
// Verify the profile overrides have been used.
$this->assertEquals(91, $config->get('weight_select_max'));
// Ensure the site configure form is used.
......
......@@ -148,7 +148,8 @@ public function testSettingsUI() {
$field_override = BaseFieldOverride::loadByName('entity_test_mul', 'entity_test_mul', 'name');
$this->assertTrue($field_override->isTranslatable(), 'Base fields can be overridden with a base field bundle override entity.');
$definitions = $entity_field_manager->getFieldDefinitions('entity_test_mul', 'entity_test_mul');
$this->assertTrue($definitions['name']->isTranslatable() && !$definitions['user_id']->isTranslatable(), 'Base field bundle overrides were correctly altered.');
$this->assertTrue($definitions['name']->isTranslatable());
$this->assertFalse($definitions['user_id']->isTranslatable());
// Test that language settings are correctly stored.
$language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_article');
......
......@@ -62,7 +62,8 @@ public function testNoEditorAvailable() {
$roles_pos = strpos($raw_content, 'Roles');
$editor_pos = strpos($raw_content, 'Text editor');
$filters_pos = strpos($raw_content, 'Enabled filters');
$this->assertTrue($roles_pos < $editor_pos && $editor_pos < $filters_pos, '"Text Editor" select appears in the correct location of the text format configuration UI.');
$this->assertGreaterThan($roles_pos, $editor_pos);
$this->assertLessThan($filters_pos, $editor_pos);
// Verify the <select>.
$select = $this->xpath('//select[@name="editor[editor]"]');
......
......@@ -266,8 +266,9 @@ public function testPurgeWithDeletedAndActiveField() {
// The field still exists, deleted, with the same field name.
$fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $deleted_field_uuid, 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->isDeleted(), 'The field exists and is deleted');
$this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->getName() == $field_name);
$this->assertArrayHasKey($deleted_field_uuid, $fields);
$this->assertTrue($fields[$deleted_field_uuid]->isDeleted());
$this->assertSame($field_name, $fields[$deleted_field_uuid]->getName());
for ($i = 0; $i < 10; $i++) {
$entity = $this->container->get('entity_type.manager')
......@@ -397,7 +398,8 @@ public function testPurgeFieldStorage() {
// The field still exists, deleted.
$fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
$this->assertArrayHasKey($field->uuid(), $fields);
$this->assertTrue($fields[$field->uuid()]->isDeleted());
// Purge again to purge the field.
field_purge_batch(0);
......@@ -407,7 +409,8 @@ public function testPurgeFieldStorage() {
$this->assertCount(0, $fields, 'The field is purged.');
// The field storage still exists, not deleted.
$storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($storages[$field_storage->uuid()]) && !$storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is not deleted');
$this->assertArrayHasKey($field_storage->uuid(), $storages);
$this->assertFalse($storages[$field_storage->uuid()]->isDeleted());
// Delete the second field.
$bundle = next($this->bundles);
......@@ -429,9 +432,11 @@ public function testPurgeFieldStorage() {
// The field and the storage still exist, deleted.
$fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
$this->assertArrayHasKey($field->uuid(), $fields);
$this->assertTrue($fields[$field->uuid()]->isDeleted());
$storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($storages[$field_storage->uuid()]) && $storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is deleted');
$this->assertArrayHasKey($field_storage->uuid(), $storages);
$this->assertTrue($storages[$field_storage->uuid()]->isDeleted());
// Purge again to purge the field and the storage.
field_purge_batch(0);
......
......@@ -284,7 +284,7 @@ public function testDeleteFieldNoData() {
// Test that the first field is not deleted, and then delete it.
$field = current(\Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE]));
$this->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.');
$this->assertFalse($field->isDeleted());
$field->delete();
// Make sure the field was deleted without being marked for purging as there
......@@ -298,7 +298,7 @@ public function testDeleteFieldNoData() {
// Make sure the other field is not deleted.
$another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
$this->assertTrue(!empty($another_field) && !$another_field->isDeleted(), 'A non-deleted field is not marked for deletion.');
$this->assertFalse($another_field->isDeleted());
}
/**
......
......@@ -319,7 +319,7 @@ public function testDeleteNoData() {
// Test that the first field is not deleted, and then delete it.
$field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$field_storage = current($field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE]));
$this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage is not marked for deletion.');
$this->assertFalse($field_storage->isDeleted());
FieldStorageConfig::loadByName('entity_test', $field_storage_definition['field_name'])->delete();
// Make sure that the field storage is deleted as it had no data.
......@@ -341,18 +341,18 @@ public function testDeleteNoData() {
// Make sure the other field and its storage are not deleted.
$another_field_storage = FieldStorageConfig::load('entity_test.' . $another_field_storage_definition['field_name']);
$this->assertTrue(!empty($another_field_storage) && !$another_field_storage->isDeleted(), 'A non-deleted storage is not marked for deletion.');
$this->assertFalse($another_field_storage->isDeleted());
$another_field = FieldConfig::load('entity_test.' . $another_field_definition['bundle'] . '.' . $another_field_definition['field_name']);
$this->assertTrue(!empty($another_field) && !$another_field->isDeleted(), 'A field whose storage was not deleted is not marked for deletion.');
$this->assertFalse($another_field->isDeleted());
// Try to create a new field the same name as a deleted field and
// write data into it.
FieldStorageConfig::create($field_storage_definition)->save();
FieldConfig::create($field_definition)->save();
$field_storage = FieldStorageConfig::load('entity_test.' . $field_storage_definition['field_name']);
$this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage with a previously used name is created.');
$this->assertFalse($field_storage->isDeleted());
$field = FieldConfig::load('entity_test.' . $field_definition['bundle'] . '.' . $field_definition['field_name']);
$this->assertTrue(!empty($field) && !$field->isDeleted(), 'A new field for a previously used field name is created.');
$this->assertFalse($field->isDeleted());
// Save an entity with data for the field
$entity = EntityTest::create();
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\language\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
......@@ -72,21 +71,24 @@ public function testDefaultConfiguration() {
$last = $this->container->get('state')->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()]));
$this->assertSame($last_content_language, $last_interface_language);
$this->assertSame($translation->language()->getId(), $last_content_language);
$translation = $this->entity->getTranslation('es');
$this->drupalGet($translation->toUrl());
$last = $this->container->get('state')->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()]));
$this->assertSame($last_content_language, $last_interface_language);
$this->assertSame($translation->language()->getId(), $last_content_language);
$translation = $this->entity->getTranslation('fr');
$this->drupalGet($translation->toUrl());
$last = $this->container->get('state')->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()]));
$this->assertSame($last_content_language, $last_interface_language);
$this->assertSame($translation->language()->getId(), $last_content_language);
}
/**
......@@ -137,7 +139,11 @@ public function testEnabledLanguageContentNegotiator() {
$last = $this->container->get('state')->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this->assertTrue(($last_interface_language == $default_site_langcode) && ($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), 'Interface language and Content language are the same as the default translation language of the entity.');
// Check that interface language and content language are the same as the
// default translation language of the entity.
$this->assertSame($default_site_langcode, $last_interface_language);
$this->assertSame($last_content_language, $last_interface_language);
$this->assertSame($translation->language()->getId(), $last_content_language);
$this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
$this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.');
......
......@@ -139,7 +139,9 @@ public function testConfigTranslation() {
$string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
$this->assertNotEmpty($string, 'Configuration strings have been created upon installation.');
$locations = $string->getLocations();
$this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');
// Check the configuration string has been created with the right location.
$this->assertArrayHasKey('configuration', $locations);
$this->assertArrayHasKey('image.style.medium', $locations['configuration']);
// Check the string is unique and has no translation yet.
$translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
......
......@@ -103,8 +103,8 @@ public function testFileParsing() {
$this->assertTrue(isset($source_strings[$str]), new FormattableMarkup('Found source string: %source', $args));
// Make sure that the proper context was matched.
$message = $context ? new FormattableMarkup('Context for %source is %context', $args) : new FormattableMarkup('Context for %source is blank', $args);
$this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
$this->assertArrayHasKey($str, $source_strings);
$this->assertSame($context, $source_strings[$str]);
}
$this->assertSame(count($test_strings), count($source_strings), 'Found correct number of source strings.');
......
......@@ -141,13 +141,15 @@ public function testStringTranslation() {
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this->assertRaw($translation_to_en);
$this->assertTrue($name != $translation && t($name, [], ['langcode' => $langcode]) == $translation, 't() works for non-English.');
$this->assertNotEquals($translation, $name);
$this->assertEquals($translation, t($name, [], ['langcode' => $langcode]), 't() works for non-English.');
// Refresh the locale() cache to get fresh data from t() below. We are in
// the same HTTP request and therefore t() is not refreshed by saving the
// translation above.
$this->container->get('string_translation')->reset();
// Now we should get the proper fresh translation from t().
$this->assertTrue($name != $translation_to_en && t($name, [], ['langcode' => 'en']) == $translation_to_en, 't() works for English.');
$this->assertNotEquals($translation_to_en, $name);
$this->assertEquals($translation_to_en, t($name, [], ['langcode' => 'en']), 't() works for English.');
$this->assertTrue(t($name, [], ['langcode' => LanguageInterface::LANGCODE_SYSTEM]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
$search = [
......
......@@ -147,11 +147,17 @@ public function testStringSearchApi() {
// Try quick search function with different field combinations.
$langcode = 'es';
$found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source1->source, 'context' => $source1->context]);
$this->assertTrue($found && isset($found->language) && isset($found->translation) && !$found->isNew(), 'Translation not found searching by source and context.');
$this->assertNotNull($found, 'Translation not found searching by source and context.');
$this->assertNotNull($found->language);
$this->assertNotNull($found->translation);
$this->assertFalse($found->isNew());
$this->assertEquals($translate1[$langcode]->translation, $found->translation);
// Now try a translation not found.
$found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source3->source, 'context' => $source3->context]);
$this->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found->isNew());
$this->assertNotNull($found);
$this->assertSame($source3->lid, $found->lid);
$this->assertNull($found->translation);
$this->assertTrue($found->isNew());
// Load all translations. For next queries we'll be loading only translated
// strings.
......
......@@ -83,7 +83,8 @@ public function testMultilingualNodeForm() {
// Check that the node exists in the database.
$node = $this->drupalGetNodeByTitle($edit[$title_key]);
$this->assertNotEmpty($node, 'Node found in database.');
$this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly set.');
$this->assertSame($langcode, $node->language()->getId());
$this->assertSame($body_value, $node->body->value);
// Change node language.
$langcode = 'it';
......@@ -95,7 +96,8 @@ public function testMultilingualNodeForm() {
$this->drupalPostForm(NULL, $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit[$title_key], TRUE);
$this->assertNotEmpty($node, 'Node found in database.');
$this->assertTrue($node->language()->getId() == $langcode && $node->body->value == $body_value, 'Field language correctly changed.');
$this->assertSame($langcode, $node->language()->getId());
$this->assertSame($body_value, $node->body->value);
// Enable content language URL detection.
$this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_CONTENT, [LanguageNegotiationUrl::METHOD_ID => 0]);
......
......@@ -1210,9 +1210,14 @@ protected function assertPatchProtectedFieldNamesStructure() {
$is_null_or_string = function ($value) {
return is_null($value) || is_string($value);
};
$keys_are_field_names = Inspector::assertAllStrings(array_keys(static::$patchProtectedFieldNames));
$values_are_expected_access_denied_reasons = Inspector::assertAll($is_null_or_string, static::$patchProtectedFieldNames);
$this->assertTrue($keys_are_field_names && $values_are_expected_access_denied_reasons, 'In Drupal 8.6, the structure of $patchProtectedFieldNames changed. It used to be an array with field names as values. Now those values are the keys, and their values should be either NULL or a string: a string containing the reason for why the field cannot be PATCHed, or NULL otherwise.');
$this->assertTrue(
Inspector::assertAllStrings(array_keys(static::$patchProtectedFieldNames)),
'In Drupal 8.6, the structure of $patchProtectedFieldNames changed. It used to be an array with field names as values. Now those values are the keys, and their values should be either NULL or a string: a string containing the reason for why the field cannot be PATCHed, or NULL otherwise.'
);
$this->assertTrue(
Inspector::assertAll($is_null_or_string, static::$patchProtectedFieldNames),
'In Drupal 8.6, the structure of $patchProtectedFieldNames changed. It used to be an array with field names as values. Now those values are the keys, and their values should be either NULL or a string: a string containing the reason for why the field cannot be PATCHed, or NULL otherwise.'
);
}
/**
......
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