Issue #3402292 by mstrelan, ankithashetty, dww, smustgrave: Fix strict type errors: Convert FormattableMarkup to strings (simple replacement) in core/tests/Drupal/KernelTests/*

merge-requests/5799/merge
Dave Long 2024-01-12 18:14:24 +00:00
parent 8310b5cc76
commit 7835ce45f3
No known key found for this signature in database
GPG Key ID: ED52AE211E142771
9 changed files with 168 additions and 183 deletions

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Database;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Schema;
@ -342,8 +341,8 @@ abstract class DriverSpecificSchemaTestBase extends DriverSpecificKernelTestBase
// Finally, check each column and try to insert invalid values into them.
foreach ($table_spec['fields'] as $column_name => $column_spec) {
$this->assertTrue($this->schema->fieldExists($table_name, $column_name), new FormattableMarkup('Unsigned @type column was created.', ['@type' => $column_spec['type']]));
$this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), new FormattableMarkup('Unsigned @type column rejected a negative value.', ['@type' => $column_spec['type']]));
$this->assertTrue($this->schema->fieldExists($table_name, $column_name), "Unsigned {$column_spec['type']} column was created.");
$this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), "Unsigned {$column_spec['type']} column rejected a negative value.");
}
}

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Database;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Database\RowCountException;
@ -108,7 +107,7 @@ class SelectComplexTest extends DatabaseTestBase {
];
foreach ($correct_results as $task => $count) {
$this->assertEquals($count, $records[$task], new FormattableMarkup("Correct number of '@task' records found.", ['@task' => $task]));
$this->assertEquals($count, $records[$task], "Correct number of '$task' records found.");
}
$this->assertEquals(6, $num_records, 'Returned the correct number of total rows.');
@ -143,7 +142,7 @@ class SelectComplexTest extends DatabaseTestBase {
];
foreach ($correct_results as $task => $count) {
$this->assertEquals($count, $records[$task], new FormattableMarkup("Correct number of '@task' records found.", ['@task' => $task]));
$this->assertEquals($count, $records[$task], "Correct number of '$task' records found.");
}
$this->assertEquals(1, $num_records, 'Returned the correct number of total rows.');

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\entity_test\Entity\EntityTest;
@ -69,25 +68,25 @@ class EntityApiTest extends EntityKernelTestBase {
->getStorage($entity_type);
$entities = array_values($storage->loadByProperties(['name' => 'test']));
$this->assertEquals('test', $entities[0]->name->value, new FormattableMarkup('%entity_type: Created and loaded entity', ['%entity_type' => $entity_type]));
$this->assertEquals('test', $entities[1]->name->value, new FormattableMarkup('%entity_type: Created and loaded entity', ['%entity_type' => $entity_type]));
$this->assertEquals('test', $entities[0]->name->value, "$entity_type: Created and loaded entity");
$this->assertEquals('test', $entities[1]->name->value, "$entity_type: Created and loaded entity");
// Test loading a single entity.
$loaded_entity = $storage->load($entity->id());
$this->assertEquals($entity->id(), $loaded_entity->id(), new FormattableMarkup('%entity_type: Loaded a single entity by id.', ['%entity_type' => $entity_type]));
$this->assertEquals($entity->id(), $loaded_entity->id(), "$entity_type: Loaded a single entity by id.");
// Test deleting an entity.
$entities = array_values($storage->loadByProperties(['name' => 'test2']));
$entities[0]->delete();
$entities = array_values($storage->loadByProperties(['name' => 'test2']));
$this->assertEquals([], $entities, new FormattableMarkup('%entity_type: Entity deleted.', ['%entity_type' => $entity_type]));
$this->assertEquals([], $entities, "$entity_type: Entity deleted.");
// Test updating an entity.
$entities = array_values($storage->loadByProperties(['name' => 'test']));
$entities[0]->name->value = 'test3';
$entities[0]->save();
$entity = $storage->load($entities[0]->id());
$this->assertEquals('test3', $entity->name->value, new FormattableMarkup('%entity_type: Entity updated.', ['%entity_type' => $entity_type]));
$this->assertEquals('test3', $entity->name->value, "$entity_type: Entity updated.");
// Try deleting multiple test entities by deleting all.
$entities = $storage->loadMultiple();

View File

@ -3,7 +3,6 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Render\FormattableMarkup;
/**
* Tests default values for entity fields.
@ -52,8 +51,8 @@ class EntityFieldDefaultValueTest extends EntityKernelTestBase {
->create();
$definition = $this->entityTypeManager->getDefinition($entity_type_id);
$langcode_key = $definition->getKey('langcode');
$this->assertEquals('en', $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Default language', ['%entity_type' => $entity_type_id]));
$this->assertTrue(Uuid::isValid($entity->uuid->value), new FormattableMarkup('%entity_type: Default UUID', ['%entity_type' => $entity_type_id]));
$this->assertEquals('en', $entity->{$langcode_key}->value, "$entity_type_id: Default language");
$this->assertTrue(Uuid::isValid($entity->uuid->value), "$entity_type_id: Default UUID");
$this->assertEquals([], $entity->name->getValue(), 'Field has one empty value by default.');
}

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
@ -157,63 +156,63 @@ class EntityFieldTest extends EntityKernelTestBase {
$this->assertInstanceOf(FieldItemListInterface::class, $entity->name);
$this->assertInstanceOf(FieldItemInterface::class, $entity->name[0]);
$this->assertEquals($this->entityName, $entity->name->value, new FormattableMarkup('%entity_type: Name value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityName, $entity->name[0]->value, new FormattableMarkup('%entity_type: Name value can be read through list access.', ['%entity_type' => $entity_type]));
$this->assertEquals([0 => ['value' => $this->entityName]], $entity->name->getValue(), new FormattableMarkup('%entity_type: Plain field value returned.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityName, $entity->name->value, "$entity_type: Name value can be read.");
$this->assertEquals($this->entityName, $entity->name[0]->value, "$entity_type: Name value can be read through list access.");
$this->assertEquals([0 => ['value' => $this->entityName]], $entity->name->getValue(), "$entity_type: Plain field value returned.");
// Change the name.
$new_name = $this->randomMachineName();
$entity->name->value = $new_name;
$this->assertEquals($new_name, $entity->name->value, new FormattableMarkup('%entity_type: Name can be updated and read.', ['%entity_type' => $entity_type]));
$this->assertEquals([0 => ['value' => $new_name]], $entity->name->getValue(), new FormattableMarkup('%entity_type: Plain field value reflects the update.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_name, $entity->name->value, "$entity_type: Name can be updated and read.");
$this->assertEquals([0 => ['value' => $new_name]], $entity->name->getValue(), "$entity_type: Plain field value reflects the update.");
$new_name = $this->randomMachineName();
$entity->name[0]->value = $new_name;
$this->assertEquals($new_name, $entity->name->value, new FormattableMarkup('%entity_type: Name can be updated and read through list access.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_name, $entity->name->value, "$entity_type: Name can be updated and read through list access.");
// Access the user field.
$this->assertInstanceOf(FieldItemListInterface::class, $entity->user_id);
$this->assertInstanceOf(FieldItemInterface::class, $entity->user_id[0]);
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: User name can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, "$entity_type: User id can be read.");
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: User name can be read.");
// Change the assigned user by entity.
$new_user1 = $this->createUser();
$entity->user_id->entity = $new_user1;
$this->assertEquals($new_user1->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: Updated user id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user1->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: Updated username value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user1->id(), $entity->user_id->target_id, "$entity_type: Updated user id can be read.");
$this->assertEquals($new_user1->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: Updated username value can be read.");
// Change the assigned user by id.
$new_user2 = $this->createUser();
$entity->user_id->target_id = $new_user2->id();
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: Updated user id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: Updated username value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, "$entity_type: Updated user id can be read.");
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: Updated username value can be read.");
// Try unsetting a field property.
$entity->name->value = NULL;
$entity->user_id->target_id = NULL;
$this->assertNull($entity->name->value, new FormattableMarkup('%entity_type: Name field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->user_id->target_id, new FormattableMarkup('%entity_type: User ID field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->user_id->entity, new FormattableMarkup('%entity_type: User entity field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->name->value, "$entity_type: Name field is not set.");
$this->assertNull($entity->user_id->target_id, "$entity_type: User ID field is not set.");
$this->assertNull($entity->user_id->entity, "$entity_type: User entity field is not set.");
// Test setting the values via the typed data API works as well.
// Change the assigned user by entity.
$entity->user_id->first()->get('entity')->setValue($new_user2);
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: Updated user id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: Updated user name value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, "$entity_type: Updated user id can be read.");
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: Updated user name value can be read.");
// Change the assigned user by id.
$entity->user_id->first()->get('target_id')->setValue($new_user2->id());
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: Updated user id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: Updated user name value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->id(), $entity->user_id->target_id, "$entity_type: Updated user id can be read.");
$this->assertEquals($new_user2->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: Updated user name value can be read.");
// Try unsetting a field.
$entity->name->first()->get('value')->setValue(NULL);
$entity->user_id->first()->get('target_id')->setValue(NULL);
$this->assertNull($entity->name->value, new FormattableMarkup('%entity_type: Name field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->user_id->target_id, new FormattableMarkup('%entity_type: User ID field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->user_id->entity, new FormattableMarkup('%entity_type: User entity field is not set.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->name->value, "$entity_type: Name field is not set.");
$this->assertNull($entity->user_id->target_id, "$entity_type: User ID field is not set.");
$this->assertNull($entity->user_id->entity, "$entity_type: User entity field is not set.");
// Create a fresh entity so target_id does not get its property object
// instantiated, then verify setting a new value via typed data API works.
@ -224,38 +223,38 @@ class EntityFieldTest extends EntityKernelTestBase {
]);
// Access the property object, and set a value.
$entity2->user_id->first()->get('target_id')->setValue($new_user2->id());
$this->assertEquals($new_user2->id(), $entity2->user_id->target_id, new FormattableMarkup('%entity_type: Updated user id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->name->value, $entity2->user_id->entity->name->value, new FormattableMarkup('%entity_type: Updated user name value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_user2->id(), $entity2->user_id->target_id, "$entity_type: Updated user id can be read.");
$this->assertEquals($new_user2->name->value, $entity2->user_id->entity->name->value, "$entity_type: Updated user name value can be read.");
// Test using isset(), empty() and unset().
$entity->name->value = 'test unset';
unset($entity->name->value);
$this->assertFalse(isset($entity->name->value), new FormattableMarkup('%entity_type: Name is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[0]->value), new FormattableMarkup('%entity_type: Name is not set.', ['%entity_type' => $entity_type]));
$this->assertEmpty($entity->name->value, new FormattableMarkup('%entity_type: Name is empty.', ['%entity_type' => $entity_type]));
$this->assertEmpty($entity->name[0]->value, new FormattableMarkup('%entity_type: Name is empty.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name->value), "$entity_type: Name is not set.");
$this->assertFalse(isset($entity->name[0]->value), "$entity_type: Name is not set.");
$this->assertEmpty($entity->name->value, "$entity_type: Name is empty.");
$this->assertEmpty($entity->name[0]->value, "$entity_type: Name is empty.");
$entity->name->value = 'a value';
$this->assertTrue(isset($entity->name->value), new FormattableMarkup('%entity_type: Name is set.', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name[0]->value), new FormattableMarkup('%entity_type: Name is set.', ['%entity_type' => $entity_type]));
$this->assertNotEmpty($entity->name->value, new FormattableMarkup('%entity_type: Name is not empty.', ['%entity_type' => $entity_type]));
$this->assertNotEmpty($entity->name[0]->value, new FormattableMarkup('%entity_type: Name is not empty.', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name[0]), new FormattableMarkup('%entity_type: Name string item is set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[1]), new FormattableMarkup('%entity_type: Second name string item is not set as it does not exist', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name), new FormattableMarkup('%entity_type: Name field is set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->nameInvalid), new FormattableMarkup('%entity_type: Non-existent field is not set.', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name->value), "$entity_type: Name is set.");
$this->assertTrue(isset($entity->name[0]->value), "$entity_type: Name is set.");
$this->assertNotEmpty($entity->name->value, "$entity_type: Name is not empty.");
$this->assertNotEmpty($entity->name[0]->value, "$entity_type: Name is not empty.");
$this->assertTrue(isset($entity->name[0]), "$entity_type: Name string item is set.");
$this->assertFalse(isset($entity->name[1]), "$entity_type: Second name string item is not set as it does not exist");
$this->assertTrue(isset($entity->name), "$entity_type: Name field is set.");
$this->assertFalse(isset($entity->nameInvalid), "$entity_type: Non-existent field is not set.");
unset($entity->name[0]);
$this->assertFalse(isset($entity->name[0]), new FormattableMarkup('%entity_type: Name field item is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[0]->value), new FormattableMarkup('%entity_type: Name is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name->value), new FormattableMarkup('%entity_type: Name is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[0]), "$entity_type: Name field item is not set.");
$this->assertFalse(isset($entity->name[0]->value), "$entity_type: Name is not set.");
$this->assertFalse(isset($entity->name->value), "$entity_type: Name is not set.");
// Test emptying a field by assigning an empty value. NULL and array()
// behave the same.
foreach ([NULL, [], 'unset'] as $empty) {
// Make sure a value is present
$entity->name->value = 'a value';
$this->assertTrue(isset($entity->name->value), new FormattableMarkup('%entity_type: Name is set.', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name->value), "$entity_type: Name is set.");
// Now, empty the field.
if ($empty === 'unset') {
unset($entity->name);
@ -263,36 +262,36 @@ class EntityFieldTest extends EntityKernelTestBase {
else {
$entity->name = $empty;
}
$this->assertTrue(isset($entity->name), new FormattableMarkup('%entity_type: Name field is set.', ['%entity_type' => $entity_type]));
$this->assertTrue($entity->name->isEmpty(), new FormattableMarkup('%entity_type: Name field is set.', ['%entity_type' => $entity_type]));
$this->assertCount(0, $entity->name, new FormattableMarkup('%entity_type: Name field contains no items.', ['%entity_type' => $entity_type]));
$this->assertSame([], $entity->name->getValue(), new FormattableMarkup('%entity_type: Name field value is an empty array.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[0]), new FormattableMarkup('%entity_type: Name field item is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name[0]->value), new FormattableMarkup('%entity_type: First name item value is not set.', ['%entity_type' => $entity_type]));
$this->assertFalse(isset($entity->name->value), new FormattableMarkup('%entity_type: Name value is not set.', ['%entity_type' => $entity_type]));
$this->assertTrue(isset($entity->name), "$entity_type: Name field is set.");
$this->assertTrue($entity->name->isEmpty(), "$entity_type: Name field is set.");
$this->assertCount(0, $entity->name, "$entity_type: Name field contains no items.");
$this->assertSame([], $entity->name->getValue(), "$entity_type: Name field value is an empty array.");
$this->assertFalse(isset($entity->name[0]), "$entity_type: Name field item is not set.");
$this->assertFalse(isset($entity->name[0]->value), "$entity_type: First name item value is not set.");
$this->assertFalse(isset($entity->name->value), "$entity_type: Name value is not set.");
}
// Access the language field.
$langcode_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode');
$this->assertEquals($langcode, $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Language code can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getLanguage($langcode), $entity->{$langcode_key}->language, new FormattableMarkup('%entity_type: Language object can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($langcode, $entity->{$langcode_key}->value, "$entity_type: Language code can be read.");
$this->assertEquals(\Drupal::languageManager()->getLanguage($langcode), $entity->{$langcode_key}->language, "$entity_type: Language object can be read.");
// Change the language by code.
$entity->{$langcode_key}->value = \Drupal::languageManager()->getDefaultLanguage()->getId();
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage()->getId(), $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Language code can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage(), $entity->{$langcode_key}->language, new FormattableMarkup('%entity_type: Language object can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage()->getId(), $entity->{$langcode_key}->value, "$entity_type: Language code can be read.");
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage(), $entity->{$langcode_key}->language, "$entity_type: Language object can be read.");
// Revert language by code then try setting it by language object.
$entity->{$langcode_key}->value = $langcode;
$entity->{$langcode_key}->language = \Drupal::languageManager()->getDefaultLanguage();
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage()->getId(), $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Language code can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage(), $entity->{$langcode_key}->language, new FormattableMarkup('%entity_type: Language object can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage()->getId(), $entity->{$langcode_key}->value, "$entity_type: Language code can be read.");
$this->assertEquals(\Drupal::languageManager()->getDefaultLanguage(), $entity->{$langcode_key}->language, "$entity_type: Language object can be read.");
// Access the text field and test updating.
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, new FormattableMarkup('%entity_type: Text field can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, "$entity_type: Text field can be read.");
$new_text = $this->randomMachineName();
$entity->field_test_text->value = $new_text;
$this->assertEquals($new_text, $entity->field_test_text->value, new FormattableMarkup('%entity_type: Updated text field can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($new_text, $entity->field_test_text->value, "$entity_type: Updated text field can be read.");
// Test creating the entity by passing in plain values.
$this->entityName = $this->randomMachineName();
@ -309,70 +308,70 @@ class EntityFieldTest extends EntityKernelTestBase {
'user_id' => $user_item,
'field_test_text' => $text_item,
]);
$this->assertEquals($this->entityName, $entity->name->value, new FormattableMarkup('%entity_type: Name value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: User name can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, new FormattableMarkup('%entity_type: Text field can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityName, $entity->name->value, "$entity_type: Name value can be read.");
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, "$entity_type: User id can be read.");
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: User name can be read.");
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, "$entity_type: Text field can be read.");
// Tests copying field values by assigning the TypedData objects.
$entity2 = $this->createTestEntity($entity_type);
$entity2->name = $entity->name;
$entity2->user_id = $entity->user_id;
$entity2->field_test_text = $entity->field_test_text;
$this->assertNotSame($entity->name, $entity2->name, new FormattableMarkup('%entity_type: Copying properties results in a different field object.', ['%entity_type' => $entity_type]));
$this->assertEquals($entity->name->value, $entity2->name->value, new FormattableMarkup('%entity_type: Name field copied.', ['%entity_type' => $entity_type]));
$this->assertEquals($entity->user_id->target_id, $entity2->user_id->target_id, new FormattableMarkup('%entity_type: User id field copied.', ['%entity_type' => $entity_type]));
$this->assertEquals($entity->field_test_text->value, $entity2->field_test_text->value, new FormattableMarkup('%entity_type: Text field copied.', ['%entity_type' => $entity_type]));
$this->assertNotSame($entity->name, $entity2->name, "$entity_type: Copying properties results in a different field object.");
$this->assertEquals($entity->name->value, $entity2->name->value, "$entity_type: Name field copied.");
$this->assertEquals($entity->user_id->target_id, $entity2->user_id->target_id, "$entity_type: User id field copied.");
$this->assertEquals($entity->field_test_text->value, $entity2->field_test_text->value, "$entity_type: Text field copied.");
// Tests that assigning TypedData objects to non-field properties keeps the
// assigned value as is.
$entity2 = $this->createTestEntity($entity_type);
$entity2->_not_a_field = $entity->name;
$this->assertSame($entity->name, $entity2->_not_a_field, new FormattableMarkup('%entity_type: Typed data objects can be copied to non-field properties as is.', ['%entity_type' => $entity_type]));
$this->assertSame($entity->name, $entity2->_not_a_field, "$entity_type: Typed data objects can be copied to non-field properties as is.");
// Tests adding a value to a field item list.
$entity->name[] = 'Another name';
$this->assertEquals('Another name', $entity->name[1]->value, new FormattableMarkup('%entity_type: List item added via [] and the first property.', ['%entity_type' => $entity_type]));
$this->assertEquals('Another name', $entity->name[1]->value, "$entity_type: List item added via [] and the first property.");
$entity->name[] = ['value' => 'Third name'];
$this->assertEquals('Third name', $entity->name[2]->value, new FormattableMarkup('%entity_type: List item added via [] and an array of properties.', ['%entity_type' => $entity_type]));
$this->assertEquals('Third name', $entity->name[2]->value, "$entity_type: List item added via [] and an array of properties.");
$entity->name[3] = ['value' => 'Fourth name'];
$this->assertEquals('Fourth name', $entity->name[3]->value, new FormattableMarkup('%entity_type: List item added via offset and an array of properties.', ['%entity_type' => $entity_type]));
$this->assertEquals('Fourth name', $entity->name[3]->value, "$entity_type: List item added via offset and an array of properties.");
unset($entity->name[3]);
// Test removing and empty-ing list items.
$this->assertCount(3, $entity->name, new FormattableMarkup('%entity_type: List has 3 items.', ['%entity_type' => $entity_type]));
$this->assertCount(3, $entity->name, "$entity_type: List has 3 items.");
unset($entity->name[1]);
$this->assertCount(2, $entity->name, new FormattableMarkup('%entity_type: Second list item has been removed.', ['%entity_type' => $entity_type]));
$this->assertEquals('Third name', $entity->name[1]->value, new FormattableMarkup('%entity_type: The subsequent items have been shifted up.', ['%entity_type' => $entity_type]));
$this->assertEquals(1, $entity->name[1]->getName(), new FormattableMarkup('%entity_type: The items names have been updated to their new delta.', ['%entity_type' => $entity_type]));
$this->assertCount(2, $entity->name, "$entity_type: Second list item has been removed.");
$this->assertEquals('Third name', $entity->name[1]->value, "$entity_type: The subsequent items have been shifted up.");
$this->assertEquals(1, $entity->name[1]->getName(), "$entity_type: The items names have been updated to their new delta.");
$entity->name[1] = NULL;
$this->assertCount(2, $entity->name, new FormattableMarkup('%entity_type: Assigning NULL does not reduce array count.', ['%entity_type' => $entity_type]));
$this->assertTrue($entity->name[1]->isEmpty(), new FormattableMarkup('%entity_type: Assigning NULL empties the item.', ['%entity_type' => $entity_type]));
$this->assertCount(2, $entity->name, "$entity_type: Assigning NULL does not reduce array count.");
$this->assertTrue($entity->name[1]->isEmpty(), "$entity_type: Assigning NULL empties the item.");
// Test using isEmpty().
unset($entity->name[1]);
$this->assertFalse($entity->name[0]->isEmpty(), new FormattableMarkup('%entity_type: Name item is not empty.', ['%entity_type' => $entity_type]));
$this->assertFalse($entity->name[0]->isEmpty(), "$entity_type: Name item is not empty.");
$entity->name->value = NULL;
$this->assertTrue($entity->name[0]->isEmpty(), new FormattableMarkup('%entity_type: Name item is empty.', ['%entity_type' => $entity_type]));
$this->assertTrue($entity->name->isEmpty(), new FormattableMarkup('%entity_type: Name field is empty.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entity->name, new FormattableMarkup('%entity_type: Empty item is considered when counting.', ['%entity_type' => $entity_type]));
$this->assertCount(1, iterator_to_array($entity->name->getIterator()), new FormattableMarkup('%entity_type: Count matches iterator count.', ['%entity_type' => $entity_type]));
$this->assertSame([0 => ['value' => NULL]], $entity->name->getValue(), new FormattableMarkup('%entity_type: Name field value contains a NULL value.', ['%entity_type' => $entity_type]));
$this->assertTrue($entity->name[0]->isEmpty(), "$entity_type: Name item is empty.");
$this->assertTrue($entity->name->isEmpty(), "$entity_type: Name field is empty.");
$this->assertCount(1, $entity->name, "$entity_type: Empty item is considered when counting.");
$this->assertCount(1, iterator_to_array($entity->name->getIterator()), "$entity_type: Count matches iterator count.");
$this->assertSame([0 => ['value' => NULL]], $entity->name->getValue(), "$entity_type: Name field value contains a NULL value.");
// Test using filterEmptyItems().
$entity->name = [NULL, 'foo'];
$this->assertCount(2, $entity->name, new FormattableMarkup('%entity_type: List has 2 items.', ['%entity_type' => $entity_type]));
$this->assertCount(2, $entity->name, "$entity_type: List has 2 items.");
$entity->name->filterEmptyItems();
$this->assertCount(1, $entity->name, new FormattableMarkup('%entity_type: The empty item was removed.', ['%entity_type' => $entity_type]));
$this->assertEquals('foo', $entity->name[0]->value, new FormattableMarkup('%entity_type: The items were renumbered.', ['%entity_type' => $entity_type]));
$this->assertEquals(0, $entity->name[0]->getName(), new FormattableMarkup('%entity_type: The deltas were updated in the items.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entity->name, "$entity_type: The empty item was removed.");
$this->assertEquals('foo', $entity->name[0]->value, "$entity_type: The items were renumbered.");
$this->assertEquals(0, $entity->name[0]->getName(), "$entity_type: The deltas were updated in the items.");
// Test get and set field values.
$entity->name = 'foo';
$this->assertEquals(['value' => 'foo'], $entity->name[0]->toArray(), new FormattableMarkup('%entity_type: Field value has been retrieved via toArray()', ['%entity_type' => $entity_type]));
$this->assertEquals(['value' => 'foo'], $entity->name[0]->toArray(), "$entity_type: Field value has been retrieved via toArray()");
$values = $entity->toArray();
$this->assertEquals([0 => ['value' => 'foo']], $values['name'], new FormattableMarkup('%entity_type: Field value has been retrieved via toArray() from an entity.', ['%entity_type' => $entity_type]));
$this->assertEquals([0 => ['value' => 'foo']], $values['name'], "$entity_type: Field value has been retrieved via toArray() from an entity.");
// Make sure the user id can be set to zero.
$user_item[0]['target_id'] = 0;
@ -383,8 +382,8 @@ class EntityFieldTest extends EntityKernelTestBase {
'user_id' => $user_item,
'field_test_text' => $text_item,
]);
$this->assertNotNull($entity->user_id->target_id, new FormattableMarkup('%entity_type: User id is not NULL', ['%entity_type' => $entity_type]));
$this->assertSame(0, $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id has been set to 0', ['%entity_type' => $entity_type]));
$this->assertNotNull($entity->user_id->target_id, "$entity_type: User id is not NULL");
$this->assertSame(0, $entity->user_id->target_id, "$entity_type: User id has been set to 0");
// Test setting the ID with the value only.
$entity = $this->container->get('entity_type.manager')
@ -394,8 +393,8 @@ class EntityFieldTest extends EntityKernelTestBase {
'user_id' => 0,
'field_test_text' => $text_item,
]);
$this->assertNotNull($entity->user_id->target_id, new FormattableMarkup('%entity_type: User id is not NULL', ['%entity_type' => $entity_type]));
$this->assertSame(0, $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id has been set to 0', ['%entity_type' => $entity_type]));
$this->assertNotNull($entity->user_id->target_id, "$entity_type: User id is not NULL");
$this->assertSame(0, $entity->user_id->target_id, "$entity_type: User id has been set to 0");
}
/**
@ -418,21 +417,21 @@ class EntityFieldTest extends EntityKernelTestBase {
$langcode_key = $this->entityTypeManager->getDefinition($entity_type)->getKey('langcode');
$entity = $this->createTestEntity($entity_type);
$entity->save();
$this->assertTrue((bool) $entity->id(), new FormattableMarkup('%entity_type: Entity has received an id.', ['%entity_type' => $entity_type]));
$this->assertTrue((bool) $entity->id(), "$entity_type: Entity has received an id.");
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->load($entity->id());
$this->assertTrue((bool) $entity->id(), new FormattableMarkup('%entity_type: Entity loaded.', ['%entity_type' => $entity_type]));
$this->assertTrue((bool) $entity->id(), "$entity_type: Entity loaded.");
// Access the name field.
$this->assertEquals(1, $entity->id->value, new FormattableMarkup('%entity_type: ID value can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(1, $entity->id->value, "$entity_type: ID value can be read.");
$this->assertIsString($entity->uuid->value);
$this->assertEquals('en', $entity->{$langcode_key}->value, new FormattableMarkup('%entity_type: Language code can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getLanguage('en'), $entity->{$langcode_key}->language, new FormattableMarkup('%entity_type: Language object can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, new FormattableMarkup('%entity_type: User id can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, new FormattableMarkup('%entity_type: User name can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, new FormattableMarkup('%entity_type: Text field can be read.', ['%entity_type' => $entity_type]));
$this->assertEquals('en', $entity->{$langcode_key}->value, "$entity_type: Language code can be read.");
$this->assertEquals(\Drupal::languageManager()->getLanguage('en'), $entity->{$langcode_key}->language, "$entity_type: Language object can be read.");
$this->assertEquals($this->entityUser->id(), $entity->user_id->target_id, "$entity_type: User id can be read.");
$this->assertEquals($this->entityUser->getAccountName(), $entity->user_id->entity->name->value, "$entity_type: User name can be read.");
$this->assertEquals($this->entityFieldText, $entity->field_test_text->value, "$entity_type: Text field can be read.");
}
/**
@ -574,8 +573,8 @@ class EntityFieldTest extends EntityKernelTestBase {
}
$fields = $entity->getFields();
$this->assertEquals(array_keys($entity->getTypedData()->getDataDefinition()->getPropertyDefinitions()), array_keys($fields), new FormattableMarkup('%entity_type: All fields returned.', ['%entity_type' => $entity_type]));
$this->assertEquals(iterator_to_array($entity->getIterator()), $fields, new FormattableMarkup('%entity_type: Entity iterator iterates over all fields.', ['%entity_type' => $entity_type]));
$this->assertEquals(array_keys($entity->getTypedData()->getDataDefinition()->getPropertyDefinitions()), array_keys($fields), "$entity_type: All fields returned.");
$this->assertEquals(iterator_to_array($entity->getIterator()), $fields, "$entity_type: Entity iterator iterates over all fields.");
}
/**
@ -623,7 +622,7 @@ class EntityFieldTest extends EntityKernelTestBase {
asort($strings);
asort($target_strings);
$this->assertEquals(array_values($target_strings), array_values($strings), new FormattableMarkup('%entity_type: All contained strings found.', ['%entity_type' => $entity_type]));
$this->assertEquals(array_values($target_strings), array_values($strings), "$entity_type: All contained strings found.");
}
/**
@ -950,14 +949,14 @@ class EntityFieldTest extends EntityKernelTestBase {
$entity->field_test_text->format = filter_default_format();
$target = "<p>The &lt;strong&gt;text&lt;/strong&gt; text to filter.</p>\n";
$this->assertSame($target, (string) $entity->field_test_text->processed, new FormattableMarkup('%entity_type: Text is processed with the default filter.', ['%entity_type' => $entity_type]));
$this->assertSame($target, (string) $entity->field_test_text->processed, "$entity_type: Text is processed with the default filter.");
// Save and load entity and make sure it still works.
$entity->save();
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->load($entity->id());
$this->assertSame($target, (string) $entity->field_test_text->processed, new FormattableMarkup('%entity_type: Text is processed with the default filter.', ['%entity_type' => $entity_type]));
$this->assertSame($target, (string) $entity->field_test_text->processed, "$entity_type: Text is processed with the default filter.");
}
/**

View File

@ -4,7 +4,6 @@ namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
@ -113,27 +112,27 @@ class EntitySchemaTest extends EntityKernelTestBase {
// Initially only the base table and the dedicated field data table should
// exist.
foreach ($tables as $index => $table) {
$this->assertEquals(!$index, $schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
$this->assertEquals(!$index, $schema_handler->tableExists($table), "Entity schema correct for the $table table.");
}
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), "Field schema correct for the $table table.");
// Update the entity type definition and check that the entity schema now
// supports translations and revisions.
$this->updateEntityType(TRUE);
foreach ($tables as $table) {
$this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
$this->assertTrue($schema_handler->tableExists($table), "Entity schema correct for the $table table.");
}
foreach ($dedicated_tables as $table) {
$this->assertTrue($schema_handler->tableExists($table), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
$this->assertTrue($schema_handler->tableExists($table), "Field schema correct for the $table table.");
}
// Revert changes and check that the entity schema now does not support
// neither translations nor revisions.
$this->updateEntityType(FALSE);
foreach ($tables as $index => $table) {
$this->assertEquals(!$index, $schema_handler->tableExists($table), new FormattableMarkup('Entity schema correct for the @table table.', ['@table' => $table]));
$this->assertEquals(!$index, $schema_handler->tableExists($table), "Entity schema correct for the $table table.");
}
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), new FormattableMarkup('Field schema correct for the @table table.', ['@table' => $table]));
$this->assertTrue($schema_handler->tableExists($dedicated_tables[0]), "Field schema correct for the $table table.");
}
/**

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\TranslationStatusInterface;
use Drupal\entity_test\Entity\EntityTestMul;
@ -42,7 +41,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
'name' => 'test',
'user_id' => $this->container->get('current_user')->id(),
]);
$this->assertEquals($this->languageManager->getDefaultLanguage()->getId(), $entity->language()->getId(), new FormattableMarkup('%entity_type: Entity created with API has default language.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->languageManager->getDefaultLanguage()->getId(), $entity->language()->getId(), "$entity_type: Entity created with API has default language.");
$entity = $this->container->get('entity_type.manager')
->getStorage($entity_type)
->create([
@ -51,15 +50,15 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->language()->getId(), new FormattableMarkup('%entity_type: Entity language not specified.', ['%entity_type' => $entity_type]));
$this->assertEmpty($entity->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', ['%entity_type' => $entity_type]));
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->language()->getId(), "$entity_type: Entity language not specified.");
$this->assertEmpty($entity->getTranslationLanguages(FALSE), "$entity_type: No translations are available");
// Set the value in default language.
$entity->set($this->fieldName, [0 => ['value' => 'default value']]);
// Get the value.
$field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get($this->fieldName);
$this->assertEquals('default value', $field->value, new FormattableMarkup('%entity_type: Untranslated value retrieved.', ['%entity_type' => $entity_type]));
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $field->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals('default value', $field->value, "$entity_type: Untranslated value retrieved.");
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $field->getLangcode(), "$entity_type: Field object has the expected langcode.");
// Try to get add a translation to language neutral entity.
try {
@ -75,21 +74,21 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$default_langcode = $this->langcodes[0];
$entity->{$langcode_key}->value = $default_langcode;
$entity->{$this->fieldName} = [];
$this->assertEquals(\Drupal::languageManager()->getLanguage($this->langcodes[0]), $entity->language(), new FormattableMarkup('%entity_type: Entity language retrieved.', ['%entity_type' => $entity_type]));
$this->assertEmpty($entity->getTranslationLanguages(FALSE), new FormattableMarkup('%entity_type: No translations are available', ['%entity_type' => $entity_type]));
$this->assertEquals(\Drupal::languageManager()->getLanguage($this->langcodes[0]), $entity->language(), "$entity_type: Entity language retrieved.");
$this->assertEmpty($entity->getTranslationLanguages(FALSE), "$entity_type: No translations are available");
// Set the value in default language.
$entity->set($this->fieldName, [0 => ['value' => 'default value']]);
// Get the value.
$field = $entity->get($this->fieldName);
$this->assertEquals('default value', $field->value, new FormattableMarkup('%entity_type: Untranslated value retrieved.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals('default value', $field->value, "$entity_type: Untranslated value retrieved.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: Field object has the expected langcode.");
// Set a translation.
$entity->addTranslation($this->langcodes[1])->set($this->fieldName, [0 => ['value' => 'translation 1']]);
$field = $entity->getTranslation($this->langcodes[1])->{$this->fieldName};
$this->assertEquals('translation 1', $field->value, new FormattableMarkup('%entity_type: Translated value set.', ['%entity_type' => $entity_type]));
$this->assertEquals($this->langcodes[1], $field->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals('translation 1', $field->value, "$entity_type: Translated value set.");
$this->assertEquals($this->langcodes[1], $field->getLangcode(), "$entity_type: Field object has the expected langcode.");
// Make sure the untranslated value stays.
$field = $entity->get($this->fieldName);
@ -109,7 +108,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
}
// Try to get a not available translation.
$this->assertNull($entity->addTranslation($this->langcodes[2])->get($this->fieldName)->value, new FormattableMarkup('%entity_type: A translation that is not available is NULL.', ['%entity_type' => $entity_type]));
$this->assertNull($entity->addTranslation($this->langcodes[2])->get($this->fieldName)->value, "$entity_type: A translation that is not available is NULL.");
// Try to get a value using an invalid language code.
try {
@ -123,7 +122,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
// Try to set a value using an invalid language code.
try {
$entity->getTranslation('invalid')->set($this->fieldName, NULL);
$this->fail(new FormattableMarkup('%entity_type: Setting a translation for an invalid language throws an exception.', ['%entity_type' => $entity_type]));
$this->fail("$entity_type: Setting a translation for an invalid language throws an exception.");
}
catch (\InvalidArgumentException $e) {
// Expected exception; just continue testing.
@ -134,8 +133,8 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$entity->getTranslation($this->langcodes[1])->set($field_name, [0 => ['value' => 'default value2']]);
// Get the value.
$field = $entity->get($field_name);
$this->assertEquals('default value2', $field->value, new FormattableMarkup('%entity_type: Untranslated value set into a translation in non-strict mode.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: Field object has the expected langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals('default value2', $field->value, "$entity_type: Untranslated value set into a translation in non-strict mode.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: Field object has the expected langcode.");
}
/**
@ -169,21 +168,21 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$entity->save();
$entity = $storage->load($entity->id());
$default_langcode = $entity->language()->getId();
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $default_langcode, new FormattableMarkup('%entity_type: Entity created as language neutral.', ['%entity_type' => $entity_type]));
$this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $default_langcode, "$entity_type: Entity created as language neutral.");
$field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get('name');
$this->assertEquals($name, $field->value, new FormattableMarkup('%entity_type: The entity name has been correctly stored as language neutral.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: The field object has the expect langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals($uid, $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get('user_id')->target_id, new FormattableMarkup('%entity_type: The entity author has been correctly stored as language neutral.', ['%entity_type' => $entity_type]));
$this->assertEquals($name, $field->value, "$entity_type: The entity name has been correctly stored as language neutral.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: The field object has the expect langcode.");
$this->assertEquals($uid, $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get('user_id')->target_id, "$entity_type: The entity author has been correctly stored as language neutral.");
$translation = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
$field = $translation->get('name');
$this->assertEquals($name, $field->value, new FormattableMarkup('%entity_type: The entity name defaults to neutral language.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: The field object has the expect langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals($uid, $translation->get('user_id')->target_id, new FormattableMarkup('%entity_type: The entity author defaults to neutral language.', ['%entity_type' => $entity_type]));
$this->assertEquals($name, $field->value, "$entity_type: The entity name defaults to neutral language.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: The field object has the expect langcode.");
$this->assertEquals($uid, $translation->get('user_id')->target_id, "$entity_type: The entity author defaults to neutral language.");
$field = $entity->get('name');
$this->assertEquals($name, $field->value, new FormattableMarkup('%entity_type: The entity name can be retrieved without specifying a language.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: The field object has the expect langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals($uid, $entity->get('user_id')->target_id, new FormattableMarkup('%entity_type: The entity author can be retrieved without specifying a language.', ['%entity_type' => $entity_type]));
$this->assertEquals($name, $field->value, "$entity_type: The entity name can be retrieved without specifying a language.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: The field object has the expect langcode.");
$this->assertEquals($uid, $entity->get('user_id')->target_id, "$entity_type: The entity author can be retrieved without specifying a language.");
// Create a language-aware entity and check that properties are stored
// as language-aware.
@ -193,11 +192,11 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$entity->save();
$entity = $storage->load($entity->id());
$default_langcode = $entity->language()->getId();
$this->assertEquals($langcode, $default_langcode, new FormattableMarkup('%entity_type: Entity created as language specific.', ['%entity_type' => $entity_type]));
$this->assertEquals($langcode, $default_langcode, "$entity_type: Entity created as language specific.");
$field = $entity->getTranslation($langcode)->get('name');
$this->assertEquals($name, $field->value, new FormattableMarkup('%entity_type: The entity name has been correctly stored as a language-aware property.', ['%entity_type' => $entity_type]));
$this->assertEquals($default_langcode, $field->getLangcode(), new FormattableMarkup('%entity_type: The field object has the expect langcode.', ['%entity_type' => $entity_type]));
$this->assertEquals($uid, $entity->getTranslation($langcode)->get('user_id')->target_id, new FormattableMarkup('%entity_type: The entity author has been correctly stored as a language-aware property.', ['%entity_type' => $entity_type]));
$this->assertEquals($name, $field->value, "$entity_type: The entity name has been correctly stored as a language-aware property.");
$this->assertEquals($default_langcode, $field->getLangcode(), "$entity_type: The field object has the expect langcode.");
$this->assertEquals($uid, $entity->getTranslation($langcode)->get('user_id')->target_id, "$entity_type: The entity author has been correctly stored as a language-aware property.");
// Create property translations.
$properties = [];
@ -225,15 +224,11 @@ class EntityTranslationTest extends EntityLanguageTestBase {
// Check that property translation were correctly stored.
$entity = $storage->load($entity->id());
foreach ($this->langcodes as $langcode) {
$args = [
'%entity_type' => $entity_type,
'%langcode' => $langcode,
];
$field = $entity->getTranslation($langcode)->get('name');
$this->assertEquals($properties[$langcode]['name'][0], $field->value, new FormattableMarkup('%entity_type: The entity name has been correctly stored for language %langcode.', $args));
$this->assertEquals($properties[$langcode]['name'][0], $field->value, "$entity_type: The entity name has been correctly stored for language $langcode.");
$field_langcode = ($langcode == $entity->language()->getId()) ? $default_langcode : $langcode;
$this->assertEquals($field->getLangcode(), $field_langcode, new FormattableMarkup('%entity_type: The field object has the expected langcode %langcode.', $args));
$this->assertEquals($entity->getTranslation($langcode)->get('user_id')->target_id, $properties[$langcode]['user_id'][0], new FormattableMarkup('%entity_type: The entity author has been correctly stored for language %langcode.', $args));
$this->assertEquals($field->getLangcode(), $field_langcode, "$entity_type: The field object has the expected langcode $langcode.");
$this->assertEquals($entity->getTranslation($langcode)->get('user_id')->target_id, $properties[$langcode]['user_id'][0], "$entity_type: The entity author has been correctly stored for language $langcode.");
}
// Test query conditions (cache is reset at each call).
@ -251,24 +246,24 @@ class EntityTranslationTest extends EntityLanguageTestBase {
])->save();
$entities = $storage->loadMultiple();
$this->assertCount(3, $entities, new FormattableMarkup('%entity_type: Three entities were created.', ['%entity_type' => $entity_type]));
$this->assertCount(3, $entities, "$entity_type: Three entities were created.");
$entities = $storage->loadMultiple([$translated_id]);
$this->assertCount(1, $entities, new FormattableMarkup('%entity_type: One entity correctly loaded by id.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entities, "$entity_type: One entity correctly loaded by id.");
$entities = $storage->loadByProperties(['name' => $name]);
$this->assertCount(2, $entities, new FormattableMarkup('%entity_type: Two entities correctly loaded by name.', ['%entity_type' => $entity_type]));
$this->assertCount(2, $entities, "$entity_type: Two entities correctly loaded by name.");
// @todo The default language condition should go away in favor of an
// explicit parameter.
$entities = $storage->loadByProperties(['name' => $properties[$langcode]['name'][0], $default_langcode_key => 0]);
$this->assertCount(1, $entities, new FormattableMarkup('%entity_type: One entity correctly loaded by name translation.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entities, "$entity_type: One entity correctly loaded by name translation.");
$entities = $storage->loadByProperties([$langcode_key => $default_langcode, 'name' => $name]);
$this->assertCount(1, $entities, new FormattableMarkup('%entity_type: One entity correctly loaded by name and language.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entities, "$entity_type: One entity correctly loaded by name and language.");
$entities = $storage->loadByProperties([$langcode_key => $langcode, 'name' => $properties[$langcode]['name'][0]]);
$this->assertCount(0, $entities, new FormattableMarkup('%entity_type: No entity loaded by name translation specifying the translation language.', ['%entity_type' => $entity_type]));
$this->assertCount(0, $entities, "$entity_type: No entity loaded by name translation specifying the translation language.");
$entities = $storage->loadByProperties([$langcode_key => $langcode, 'name' => $properties[$langcode]['name'][0], $default_langcode_key => 0]);
$this->assertCount(1, $entities, new FormattableMarkup('%entity_type: One entity loaded by name translation and language specifying to look for translations.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $entities, "$entity_type: One entity loaded by name translation and language specifying to look for translations.");
$entities = $storage->loadByProperties(['user_id' => $properties[$langcode]['user_id'][0], $default_langcode_key => NULL]);
$this->assertCount(2, $entities, new FormattableMarkup('%entity_type: Two entities loaded by uid without caring about property translatability.', ['%entity_type' => $entity_type]));
$this->assertCount(2, $entities, "$entity_type: Two entities loaded by uid without caring about property translatability.");
// Test property conditions and orders with multiple languages in the same
// query.
@ -280,7 +275,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
->condition($group)
->condition('name', $properties[$langcode]['name'][0], '=', $langcode)
->execute();
$this->assertCount(1, $result, new FormattableMarkup('%entity_type: One entity loaded by name and uid using different language meta conditions.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $result, "$entity_type: One entity loaded by name and uid using different language meta conditions.");
// Test mixed property and field conditions.
$storage->resetCache($result);
@ -300,7 +295,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
->condition($default_langcode_group)
->condition($langcode_group)
->execute();
$this->assertCount(1, $result, new FormattableMarkup('%entity_type: One entity loaded by name, uid and field value using different language meta conditions.', ['%entity_type' => $entity_type]));
$this->assertCount(1, $result, "$entity_type: One entity loaded by name, uid and field value using different language meta conditions.");
}
/**
@ -793,7 +788,7 @@ class EntityTranslationTest extends EntityLanguageTestBase {
foreach ($langcodes as $langcode) {
$adapter = $entity->getTranslation($langcode)->getTypedData();
$name = $adapter->get('name')->value;
$this->assertEquals($values[$langcode]['name'], $name, new FormattableMarkup('Name correctly retrieved from "@langcode" adapter', ['@langcode' => $langcode]));
$this->assertEquals($values[$langcode]['name'], $name, "Name correctly retrieved from '$langcode' adapter");
}
}
@ -815,12 +810,10 @@ class EntityTranslationTest extends EntityLanguageTestBase {
$translation = $entity->getTranslation($langcode);
foreach ($translation->getFields() as $field_name => $field) {
if ($field->getFieldDefinition()->isTranslatable()) {
$args = ['%field_name' => $field_name, '%langcode' => $langcode];
$this->assertEquals($langcode, $field->getEntity()->language()->getId(), new FormattableMarkup('Translatable field %field_name on translation %langcode has correct entity reference in translation %langcode.', $args));
$this->assertEquals($langcode, $field->getEntity()->language()->getId(), "Translatable field $field_name on translation $langcode has correct entity reference in translation $langcode.");
}
else {
$args = ['%field_name' => $field_name, '%langcode' => $langcode, '%default_langcode' => $default_langcode];
$this->assertEquals($default_langcode, $field->getEntity()->language()->getId(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode.', $args));
$this->assertEquals($default_langcode, $field->getEntity()->language()->getId(), "Non translatable field $field_name on translation $langcode has correct entity reference in the default translation $default_langcode.");
}
}
}

View File

@ -4,7 +4,6 @@ namespace Drupal\KernelTests\Core\File;
use Drupal\Component\FileSecurity\FileSecurity;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
@ -124,14 +123,14 @@ class DirectoryTest extends FileTestBase {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$path = $file_system->createFilename($basename, $directory);
$this->assertEquals($original, $path, new FormattableMarkup('New filepath %new equals %original.', ['%new' => $path, '%original' => $original]));
$this->assertEquals($original, $path, "New filepath $path equals $original.");
// Then we test against a file that already exists within that directory.
$basename = 'druplicon.png';
$original = $directory . '/' . $basename;
$expected = $directory . '/druplicon_0.png';
$path = $file_system->createFilename($basename, $directory);
$this->assertEquals($expected, $path, new FormattableMarkup('Creating a new filepath from %original equals %new (expected %expected).', ['%new' => $path, '%original' => $original, '%expected' => $expected]));
$this->assertEquals($expected, $path, "Creating a new filepath from $path equals $original (expected $expected).");
// @TODO: Finally we copy a file into a directory several times, to ensure a properly iterating filename suffix.
}

View File

@ -2,7 +2,6 @@
namespace Drupal\KernelTests\Core\Installer;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Extension\ProfileExtensionList;
use Drupal\Core\StringTranslation\Translator\FileTranslation;
use Drupal\KernelTests\KernelTestBase;
@ -33,9 +32,9 @@ class InstallerLanguageTest extends KernelTestBase {
$file_translation = new FileTranslation('core/tests/fixtures/files/translations', $this->container->get('file_system'));
foreach ($expected_translation_files as $langcode => $files_expected) {
$files_found = $file_translation->findTranslationFiles($langcode);
$this->assertSameSize($files_expected, $files_found, new FormattableMarkup('@count installer languages found.', ['@count' => count($files_expected)]));
$this->assertSameSize($files_expected, $files_found, count($files_expected) . ' installer languages found.');
foreach ($files_found as $file) {
$this->assertContains($file->filename, $files_expected, new FormattableMarkup('@file found.', ['@file' => $file->filename]));
$this->assertContains($file->filename, $files_expected, $file->filename . ' found.');
}
}
}