UserTranslationUITest.php in Drupal 9
File
core/modules/user/tests/src/Functional/UserTranslationUITest.php
View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
class UserTranslationUITest extends ContentTranslationUITestBase {
protected $name;
protected $defaultCacheContexts = [
'languages:language_interface',
'theme',
'url.query_args:_wrapper_format',
'user.permissions',
'url.site',
];
protected static $modules = [
'language',
'content_translation',
'user',
'views',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
$this->entityTypeId = 'user';
$this->testLanguageSelector = FALSE;
$this->name = $this
->randomMachineName();
parent::setUp();
\Drupal::entityTypeManager()
->getStorage('user')
->resetCache();
}
protected function getTranslatorPermissions() {
return array_merge(parent::getTranslatorPermissions(), [
'administer users',
]);
}
protected function getNewEntityValues($langcode) {
return [
'name' => $this->name,
] + parent::getNewEntityValues($langcode);
}
protected function doTestTranslationEdit() {
$storage = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId);
$storage
->resetCache([
$this->entityId,
]);
$entity = $storage
->load($this->entityId);
$languages = $this->container
->get('language_manager')
->getLanguages();
foreach ($this->langcodes as $langcode) {
if ($langcode != 'en') {
$options = [
'language' => $languages[$langcode],
];
$url = $entity
->toUrl('edit-form', $options);
$this
->drupalGet($url);
$this
->assertSession()
->pageTextContains("{$entity->getTranslation($langcode)->label()} [{$languages[$langcode]->getName()} translation]");
}
}
}
public function testTranslatedUserDeletion() {
$this
->drupalLogin($this->administrator);
$entity_id = $this
->createEntity($this
->getNewEntityValues('en'), 'en');
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityTypeId)
->load($entity_id);
$translated_entity = $entity
->addTranslation('fr');
$translated_entity
->save();
$url = $entity
->toUrl('edit-form', [
'language' => $this->container
->get('language_manager')
->getLanguage('en'),
]);
$this
->drupalGet($url);
$this
->submitForm([], 'Cancel account');
$this
->assertSession()
->statusCodeEquals(200);
}
}