class ContentTranslationConcurrencyTest

Tests concurrency in the content translation controller.

Attributes

#[CoversClass(ContentTranslationController::class)] #[Group('content_translation')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of ContentTranslationConcurrencyTest

File

core/modules/content_translation/tests/src/Kernel/ContentTranslationConcurrencyTest.php, line 22

Namespace

Drupal\Tests\content_translation\Kernel
View source
class ContentTranslationConcurrencyTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'language',
    'content_translation',
    'user',
    'entity_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('entity_test_mul');
    $this->installEntitySchema('entity_test_mul_with_bundle');
    EntityTestMulBundle::create([
      'id' => 'test',
      'label' => 'Test label',
      'description' => 'My test description',
    ])->save();
    ConfigurableLanguage::createFromLangcode('fr')->save();
  }
  
  /**
   * Tests that adding a translation that already exists redirects gracefully.
   */
  public function testConcurrentTranslationAddition() : void {
    $language_manager = $this->container
      ->get('language_manager');
    $source_lang = $language_manager->getLanguage('en');
    $target_lang = $language_manager->getLanguage('fr');
    $entity = EntityTestMul::create([
      'name' => 'English Entity',
      'type' => 'test',
      'langcode' => 'en',
    ]);
    $entity->save();
    $entity->addTranslation('fr', [
      'name' => 'French Entity',
    ])
      ->save();
    $controller = ContentTranslationController::create($this->container);
    $route_match = new RouteMatch('entity.entity_test_mul.content_translation_add', new Route('/entity-test-mul/{entity_test_mul}/translations/add/{source}/{target}'), [
      'entity_test_mul' => $entity,
    ], [
      'entity_test_mul' => $entity->id(),
    ]);
    $response = $controller->add($source_lang, $target_lang, $route_match, 'entity_test_mul');
    $this->assertInstanceOf(RedirectResponse::class, $response);
    $expected = $entity->toUrl('edit-form')
      ->toString();
    $this->assertStringContainsString($expected, $response->getTargetUrl());
    /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
    $messenger = $this->container
      ->get('messenger');
    $errors = $messenger->messagesByType('error');
    $this->assertCount(1, $errors);
    $actual_message = (string) reset($errors);
    $this->assertEquals('A translation already exists for French.', $actual_message);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.