ConfigTranslationListUiTest.php 17.2 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\config_translation\Tests;

5
use Drupal\block_content\Entity\BlockContentType;
6
use Drupal\Component\Utility\Unicode;
7
use Drupal\field\Entity\FieldConfig;
8
use Drupal\field\Entity\FieldStorageConfig;
9
use Drupal\language\Entity\ConfigurableLanguage;
10
use Drupal\simpletest\WebTestBase;
11
use Drupal\shortcut\Entity\ShortcutSet;
12
use Drupal\contact\Entity\ContactForm;
13
use Drupal\filter\Entity\FilterFormat;
14
use Drupal\taxonomy\Entity\Vocabulary;
15 16

/**
17
 * Visit all lists.
18
 *
19
 * @group config_translation
20 21 22 23 24 25 26 27 28 29 30 31 32
 * @see \Drupal\config_translation\Tests\ConfigTranslationViewListUiTest
 */
class ConfigTranslationListUiTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'block',
    'config_translation',
    'contact',
33
    'block_content',
34 35
    'field',
    'field_ui',
36
    'menu_ui',
37 38 39 40
    'node',
    'shortcut',
    'taxonomy',
    'image',
41
    'responsive_image',
42 43 44 45 46 47 48 49 50 51
    'toolbar',
  );

  /**
   * Admin user with all needed permissions.
   *
   * @var \Drupal\user\Entity\User
   */
  protected $adminUser;

52
  protected function setUp() {
53 54 55 56 57 58 59
    parent::setUp();

    $permissions = array(
      'access site-wide contact form',
      'administer blocks',
      'administer contact forms',
      'administer content types',
60
      'administer block_content fields',
61 62 63 64 65 66 67 68 69 70
      'administer filters',
      'administer menu',
      'administer node fields',
      'administer permissions',
      'administer shortcuts',
      'administer site configuration',
      'administer taxonomy',
      'administer account settings',
      'administer languages',
      'administer image styles',
71
      'administer responsive images',
72 73 74 75 76 77
      'translate configuration',
    );

    // Create and log in user.
    $this->adminUser = $this->drupalCreateUser($permissions);
    $this->drupalLogin($this->adminUser);
78 79 80

    // Enable import of translations. By default this is disabled for automated
    // tests.
81
    $this->config('locale.settings')
82 83
      ->set('translation.import_enabled', TRUE)
      ->save();
84
    $this->drupalPlaceBlock('local_tasks_block');
85 86 87 88 89 90 91 92 93 94 95
  }

  /**
   * Tests the block listing for the translate operation.
   *
   * There are no blocks placed in the testing profile. Add one, then check
   * for Translate operation.
   */
  protected function doBlockListTest() {
    // Add a test block, any block will do.
    // Set the machine name so the translate link can be built later.
96
    $id = Unicode::strtolower($this->randomMachineName(16));
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    $this->drupalPlaceBlock('system_powered_by_block', array('id' => $id));

    // Get the Block listing.
    $this->drupalGet('admin/structure/block');

    $translate_link = 'admin/structure/block/manage/' . $id . '/translate';
    // Test if the link to translate the block is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the menu listing for the translate operation.
   */
  protected function doMenuListTest() {
    // Create a test menu to decouple looking for translate operations link so
    // this does not test more than necessary.
    $this->drupalGet('admin/structure/menu/add');
    // Lowercase the machine name.
119 120
    $menu_name = Unicode::strtolower($this->randomMachineName(16));
    $label = $this->randomMachineName(16);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    $edit = array(
      'id' => $menu_name,
      'description' => '',
      'label' =>  $label,
    );
    // Create the menu by posting the form.
    $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));

    // Get the Menu listing.
    $this->drupalGet('admin/structure/menu');

    $translate_link = 'admin/structure/menu/manage/' . $menu_name . '/translate';
    // Test if the link to translate the menu is on the page.
    $this->assertLinkByHref($translate_link);

    // Check if the Link is not added if you are missing 'translate
    // configuration' permission.
    $permissions = array(
      'administer menu',
    );
    $this->drupalLogin($this->drupalCreateUser($permissions));

    // Get the Menu listing.
    $this->drupalGet('admin/structure/menu');

    $translate_link = 'admin/structure/menu/manage/' . $menu_name . '/translate';
    // Test if the link to translate the menu is NOT on the page.
    $this->assertNoLinkByHref($translate_link);

150
    // Log in as Admin again otherwise the rest will fail.
151 152 153 154 155 156 157 158 159 160 161 162 163
    $this->drupalLogin($this->adminUser);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the vocabulary listing for the translate operation.
   */
  protected function doVocabularyListTest() {
    // Create a test vocabulary to decouple looking for translate operations
    // link so this does not test more than necessary.
164
    $vocabulary = Vocabulary::create([
165 166 167
      'name' => $this->randomMachineName(),
      'description' => $this->randomMachineName(),
      'vid' => Unicode::strtolower($this->randomMachineName()),
168
    ]);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    $vocabulary->save();

    // Get the Taxonomy listing.
    $this->drupalGet('admin/structure/taxonomy');

    $translate_link = 'admin/structure/taxonomy/manage/' . $vocabulary->id() . '/translate';
    // Test if the link to translate the vocabulary is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the custom block listing for the translate operation.
   */
186
  public function doCustomContentTypeListTest() {
187 188
    // Create a test custom block type to decouple looking for translate
    // operations link so this does not test more than necessary.
189
    $block_content_type = BlockContentType::create(array(
190 191
      'id' => Unicode::strtolower($this->randomMachineName(16)),
      'label' => $this->randomMachineName(),
192 193
      'revision' => FALSE
    ));
194
    $block_content_type->save();
195 196

    // Get the custom block type listing.
197
    $this->drupalGet('admin/structure/block/block-content/types');
198

199
    $translate_link = 'admin/structure/block/block-content/manage/' . $block_content_type->id() . '/translate';
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    // Test if the link to translate the custom block type is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the contact forms listing for the translate operation.
   */
  public function doContactFormsListTest() {
    // Create a test contact form to decouple looking for translate operations
    // link so this does not test more than necessary.
214
    $contact_form = ContactForm::create([
215 216
      'id' => Unicode::strtolower($this->randomMachineName(16)),
      'label' => $this->randomMachineName(),
217
    ]);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    $contact_form->save();

    // Get the contact form listing.
    $this->drupalGet('admin/structure/contact');

    $translate_link = 'admin/structure/contact/manage/' . $contact_form->id() . '/translate';
    // Test if the link to translate the contact form is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the content type listing for the translate operation.
   */
  public function doContentTypeListTest() {
    // Create a test content type to decouple looking for translate operations
    // link so this does not test more than necessary.
238
    $content_type = $this->drupalCreateContentType(array(
239 240
      'type' => Unicode::strtolower($this->randomMachineName(16)),
      'name' => $this->randomMachineName(),
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    ));

    // Get the content type listing.
    $this->drupalGet('admin/structure/types');

    $translate_link = 'admin/structure/types/manage/' . $content_type->id() . '/translate';
    // Test if the link to translate the content type is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the formats listing for the translate operation.
   */
  public function doFormatsListTest() {
    // Create a test format to decouple looking for translate operations
    // link so this does not test more than necessary.
261
    $filter_format = FilterFormat::create(array(
262 263
      'format' => Unicode::strtolower($this->randomMachineName(16)),
      'name' => $this->randomMachineName(),
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    ));
    $filter_format->save();

    // Get the format listing.
    $this->drupalGet('admin/config/content/formats');

    $translate_link = 'admin/config/content/formats/manage/' . $filter_format->id() . '/translate';
    // Test if the link to translate the format is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the shortcut listing for the translate operation.
   */
  public function doShortcutListTest() {
    // Create a test shortcut to decouple looking for translate operations
    // link so this does not test more than necessary.
285
    $shortcut = ShortcutSet::create(array(
286
      'id' => Unicode::strtolower($this->randomMachineName(16)),
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
      'label' => $this->randomString(),
    ));
    $shortcut->save();

    // Get the shortcut listing.
    $this->drupalGet('admin/config/user-interface/shortcut');

    $translate_link = 'admin/config/user-interface/shortcut/manage/' . $shortcut->id() . '/translate';
    // Test if the link to translate the shortcut is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the role listing for the translate operation.
   */
  public function doUserRoleListTest() {
    // Create a test role to decouple looking for translate operations
    // link so this does not test more than necessary.
309
    $role_id = Unicode::strtolower($this->randomMachineName(16));
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    $this->drupalCreateRole(array(), $role_id);

    // Get the role listing.
    $this->drupalGet('admin/people/roles');

    $translate_link = 'admin/people/roles/manage/' . $role_id . '/translate';
    // Test if the link to translate the role is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the language listing for the translate operation.
   */
  public function doLanguageListTest() {
    // Create a test language to decouple looking for translate operations
    // link so this does not test more than necessary.
330
    ConfigurableLanguage::createFromLangcode('ga')->save();
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

    // Get the language listing.
    $this->drupalGet('admin/config/regional/language');

    $translate_link = 'admin/config/regional/language/edit/ga/translate';
    // Test if the link to translate the language is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the image style listing for the translate operation.
   */
  public function doImageStyleListTest() {
    // Get the image style listing.
    $this->drupalGet('admin/config/media/image-styles');

    $translate_link = 'admin/config/media/image-styles/manage/medium/translate';
    // Test if the link to translate the style is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
361
   * Tests the responsive image mapping listing for the translate operation.
362
   */
363
  public function doResponsiveImageListTest() {
364
    $edit = array();
365
    $edit['label'] = $this->randomMachineName();
366
    $edit['id'] = strtolower($edit['label']);
367
    $edit['fallback_image_style'] = 'thumbnail';
368

369 370
    $this->drupalPostForm('admin/config/media/responsive-image-style/add', $edit, t('Save'));
    $this->assertRaw(t('Responsive image style %label saved.', array('%label' => $edit['label'])));
371

372 373
    // Get the responsive image style listing.
    $this->drupalGet('admin/config/media/responsive-image-style');
374

375
    $translate_link = 'admin/config/media/responsive-image-style/' . $edit['id'] . '/translate';
376 377 378 379 380 381 382 383 384 385 386 387 388
    // Test if the link to translate the style is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests the field listing for the translate operation.
   */
  public function doFieldListTest() {
    // Create a base content type.
389
    $content_type = $this->drupalCreateContentType(array(
390 391
      'type' => Unicode::strtolower($this->randomMachineName(16)),
      'name' => $this->randomMachineName(),
392 393
    ));

394
    // Create a block content type.
395
    $block_content_type = BlockContentType::create(array(
396 397 398 399 400
      'id' => 'basic',
      'label' => 'Basic',
      'revision' => FALSE
    ));
    $block_content_type->save();
401
    $field = FieldConfig::create([
402 403 404 405 406 407
      // The field storage is guaranteed to exist because it is supplied by the
      // block_content module.
      'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
      'bundle' => $block_content_type->id(),
      'label' => 'Body',
      'settings' => array('display_summary' => FALSE),
408
    ]);
409 410
    $field->save();

411 412 413 414 415 416 417
    // Look at a few fields on a few entity types.
    $pages = array(
      array(
        'list' => 'admin/structure/types/manage/' . $content_type->id() . '/fields',
        'field' => 'node.' . $content_type->id() . '.body',
      ),
      array(
418 419
        'list' => 'admin/structure/block/block-content/manage/basic/fields',
        'field' => 'block_content.basic.body',
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
      ),
    );

    foreach ($pages as $values) {
      // Get fields listing.
      $this->drupalGet($values['list']);

      $translate_link = $values['list'] . '/' . $values['field'] . '/translate';
      // Test if the link to translate the field is on the page.
      $this->assertLinkByHref($translate_link);

      // Test if the link to translate actually goes to the translate page.
      $this->drupalGet($translate_link);
      $this->assertRaw('<th>' . t('Language') . '</th>');
    }
  }

  /**
   * Tests the date format listing for the translate operation.
   */
  public function doDateFormatListTest() {
    // Get the date format listing.
    $this->drupalGet('admin/config/regional/date-time');

    $translate_link = 'admin/config/regional/date-time/formats/manage/long/translate';
    // Test if the link to translate the format is on the page.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests a given settings page for the translate operation.
   *
   * @param string $link
   *   URL of the settings page to test.
   */
  public function doSettingsPageTest($link) {
    // Get the settings page.
    $this->drupalGet($link);

    $translate_link = $link . '/translate';
    // Test if the link to translate the settings page is present.
    $this->assertLinkByHref($translate_link);

    // Test if the link to translate actually goes to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw('<th>' . t('Language') . '</th>');
  }

  /**
   * Tests if translate link is added to operations in all configuration lists.
   */
  public function testTranslateOperationInListUi() {
    // All lists based on paths provided by the module.
    $this->doBlockListTest();
    $this->doMenuListTest();
    $this->doVocabularyListTest();
480
    $this->doCustomContentTypeListTest();
481 482 483 484 485 486 487
    $this->doContactFormsListTest();
    $this->doContentTypeListTest();
    $this->doFormatsListTest();
    $this->doShortcutListTest();
    $this->doUserRoleListTest();
    $this->doLanguageListTest();
    $this->doImageStyleListTest();
488
    $this->doResponsiveImageListTest();
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    $this->doDateFormatListTest();
    $this->doFieldListTest();

    // Views is tested in Drupal\config_translation\Tests\ConfigTranslationViewListUiTest

    // Test the maintenance settings page.
    $this->doSettingsPageTest('admin/config/development/maintenance');
    // Test the site information settings page.
    $this->doSettingsPageTest('admin/config/system/site-information');
    // Test the account settings page.
    $this->doSettingsPageTest('admin/config/people/accounts');
    // Test the RSS settings page.
    $this->doSettingsPageTest('admin/config/services/rss-publishing');
  }

}