annotate core/modules/field/tests/src/Functional/TranslationWebTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\field\Functional;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 6 use Drupal\field\Entity\FieldConfig;
Chris@0 7 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests multilanguage fields logic that require a full environment.
Chris@0 11 *
Chris@0 12 * @group field
Chris@0 13 */
Chris@0 14 class TranslationWebTest extends FieldTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['language', 'field_test', 'entity_test'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The name of the field to use in this test.
Chris@0 25 *
Chris@0 26 * @var string
Chris@0 27 */
Chris@0 28 protected $fieldName;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * The name of the entity type to use in this test.
Chris@0 32 *
Chris@0 33 * @var string
Chris@0 34 */
Chris@0 35 protected $entityTypeId = 'entity_test_mulrev';
Chris@0 36
Chris@0 37 /**
Chris@0 38 * The field storage to use in this test.
Chris@0 39 *
Chris@0 40 * @var \Drupal\field\Entity\FieldStorageConfig
Chris@0 41 */
Chris@0 42 protected $fieldStorage;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * The field to use in this test.
Chris@0 46 *
Chris@0 47 * @var \Drupal\field\Entity\FieldConfig
Chris@0 48 */
Chris@0 49 protected $field;
Chris@0 50
Chris@0 51 protected function setUp() {
Chris@0 52 parent::setUp();
Chris@0 53
Chris@17 54 $this->fieldName = mb_strtolower($this->randomMachineName() . '_field_name');
Chris@0 55
Chris@0 56 $field_storage = [
Chris@0 57 'field_name' => $this->fieldName,
Chris@0 58 'entity_type' => $this->entityTypeId,
Chris@0 59 'type' => 'test_field',
Chris@0 60 'cardinality' => 4,
Chris@0 61 ];
Chris@0 62 FieldStorageConfig::create($field_storage)->save();
Chris@0 63 $this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName);
Chris@0 64
Chris@0 65 $field = [
Chris@0 66 'field_storage' => $this->fieldStorage,
Chris@0 67 'bundle' => $this->entityTypeId,
Chris@0 68 ];
Chris@0 69 FieldConfig::create($field)->save();
Chris@0 70 $this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName);
Chris@0 71
Chris@0 72 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
Chris@0 73 ->setComponent($this->fieldName)
Chris@0 74 ->save();
Chris@0 75
Chris@0 76 for ($i = 0; $i < 3; ++$i) {
Chris@0 77 ConfigurableLanguage::create([
Chris@0 78 'id' => 'l' . $i,
Chris@0 79 'label' => $this->randomString(),
Chris@0 80 ])->save();
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Tests field translations when creating a new revision.
Chris@0 86 */
Chris@0 87 public function testFieldFormTranslationRevisions() {
Chris@0 88 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
Chris@0 89 $this->drupalLogin($web_user);
Chris@0 90
Chris@0 91 // Prepare the field translations.
Chris@0 92 field_test_entity_info_translatable($this->entityTypeId, TRUE);
Chris@0 93 $entity = $this->container->get('entity_type.manager')
Chris@0 94 ->getStorage($this->entityTypeId)
Chris@0 95 ->create();
Chris@0 96 $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
Chris@0 97 $field_name = $this->fieldStorage->getName();
Chris@0 98
Chris@0 99 // Store the field translations.
Chris@0 100 ksort($available_langcodes);
Chris@0 101 $entity->langcode->value = key($available_langcodes);
Chris@0 102 foreach ($available_langcodes as $langcode => $value) {
Chris@0 103 $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
Chris@0 104 $translation->{$field_name}->value = $value + 1;
Chris@0 105 }
Chris@0 106 $entity->save();
Chris@0 107
Chris@0 108 // Create a new revision.
Chris@0 109 $edit = [
Chris@0 110 "{$field_name}[0][value]" => $entity->{$field_name}->value,
Chris@0 111 'revision' => TRUE,
Chris@0 112 ];
Chris@0 113 $this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
Chris@0 114
Chris@0 115 // Check translation revisions.
Chris@0 116 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
Chris@0 117 $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * Check if the field translation attached to the entity revision identified
Chris@0 122 * by the passed arguments were correctly stored.
Chris@0 123 */
Chris@0 124 private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
Chris@0 125 $field_name = $this->fieldStorage->getName();
Chris@0 126 $entity = $this->container->get('entity_type.manager')
Chris@0 127 ->getStorage($this->entityTypeId)
Chris@0 128 ->loadRevision($revision_id);
Chris@0 129 foreach ($available_langcodes as $langcode => $value) {
Chris@0 130 $passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1;
Chris@0 131 $this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', ['@language' => $langcode, '@revision' => $entity->getRevisionId()]));
Chris@0 132 }
Chris@0 133 }
Chris@0 134
Chris@0 135 }