You are here

public function NameAdminTest::testAdminListFormatSettings in Name Field 8

Misc tests related to adding, updating and removing formats.

File

tests/src/Functional/NameAdminTest.php, line 193

Class

NameAdminTest
Tests for the admin settings and custom format page.

Namespace

Drupal\Tests\name\Functional

Code

public function testAdminListFormatSettings() {

  // Default settings and system settings.
  $this
    ->drupalLogin($this->adminUser);

  // The default installed formats.
  $this
    ->drupalGet('admin/config/regional/name/list');
  $row_template = [
    'title' => '//tbody/tr[{row}]/td[1]',
    'machine' => '//tbody/tr[{row}]/td[2]',
    'settings' => '//tbody/tr[{row}]/td[3]',
    // 'examples'   => '//tbody/tr[{row}]/td[4]',
    'edit' => '//tbody/tr[{row}]/td[5]//li[contains(@class, "edit")]/a',
    'edit link' => '//tbody/tr[{row}]/td[5]//li[contains(@class, "edit")]/a/@href',
    'delete' => '//tbody/tr[{row}]/td[5]//li[contains(@class, "delete")]/a',
    'delete link' => '//tbody/tr[{row}]/td[5]//li[contains(@class, "delete")]/a/@href',
  ];
  $all_values = [
    1 => [
      'title' => t('Default'),
      'machine' => 'default',
      // 'examples' => 'todo',
      'edit link' => Url::fromRoute('entity.name_list_format.edit_form', [
        'name_list_format' => 'default',
      ])
        ->toString(),
    ],
  ];
  foreach ($all_values as $id => $row) {
    $this
      ->assertRow($row, $row_template, $id);
  }
  $this
    ->drupalGet('admin/config/regional/name/list/add');

  // All bar delimiter are required.
  $values = [
    'label' => '',
    'id' => '',
    'delimiter' => '',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/list/add', $values, t('Save list format'));
  $labels = [
    t('Name'),
    t('Machine-readable name'),
  ];
  foreach ($labels as $title) {
    $this
      ->assertText(t('@field field is required', [
      '@field' => $title,
    ]));
  }
  $values = [
    'label' => 'comma',
    'id' => '1234567890abcdefghijklmnopqrstuvwxyz_',
    'delimiter' => ', ',
    'and' => 'text',
    'delimiter_precedes_last' => 'always',
    'el_al_min' => '14',
    'el_al_first' => '5',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/list/add', $values, t('Save list format'));
  $this
    ->assertNoText(t('@field field is required', [
    '@field' => t('Last delimiter type'),
  ]));
  $this
    ->assertNoText(t('@field field is required', [
    '@field' => t('Machine-readable name'),
  ]));
  $values['id'] = '%&*(';
  $this
    ->drupalPostForm('admin/config/regional/name/list/add', $values, t('Save list format'));
  $this
    ->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  $values = [
    'label' => 'default',
    'id' => 'default',
    'delimiter' => 'a',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/list/add', $values, t('Save list format'));
  $this
    ->assertText(t('The machine-readable name is already in use. It must be unique.'));
  $values = [
    'label' => 'Test label',
    'id' => 'test',
    'delimiter' => ' / ',
    'and' => 'symbol',
    'delimiter_precedes_last' => 'contextual',
    'el_al_min' => '3',
    'el_al_first' => '1',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/list/add', $values, t('Save list format'));
  $this
    ->assertText(t('Name list format Test label added.'));
  $row = [
    'title' => 'Test label',
    'machine' => 'test',
    'delimiter' => ' / ',
    'edit link' => Url::fromRoute('entity.name_list_format.edit_form', [
      'name_list_format' => 'test',
    ])
      ->toString(),
    'delete link' => Url::fromRoute('entity.name_list_format.delete_form', [
      'name_list_format' => 'test',
    ])
      ->toString(),
  ];
  $this
    ->assertRow($row, $row_template, 3);
  $summary_text = [
    'Delimiters: " / " and Ampersand (&)',
    'Reduce after 3 items and show 1 items followed by el al.',
    'Last delimiter: Contextual',
  ];
  $this
    ->assertRowContains([
    'settings' => $summary_text,
  ], $row_template, 3);
  $this
    ->drupalGet('admin/config/regional/name/list/manage/60');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('admin/config/regional/name/list/manage/60/delete');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('admin/config/regional/name/list/manage/test/delete');
  $this
    ->assertText(t('Are you sure you want to delete the custom list format @title?', [
    '@title' => $values['label'],
  ]));
  $this
    ->drupalPostForm(NULL, [], t('Delete'));
  $this
    ->assertText(t('The name list format @title has been deleted.', [
    '@title' => $values['label'],
  ]));
}