You are here

public function NameAdminTest::testAdminFormatSettings in Name Field 8

Misc tests related to adding, updating and removing formats.

File

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

Class

NameAdminTest
Tests for the admin settings and custom format page.

Namespace

Drupal\Tests\name\Functional

Code

public function testAdminFormatSettings() {

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

  // The default installed formats.
  $this
    ->drupalGet('admin/config/regional/name');
  $row_template = [
    'title' => '//tbody/tr[{row}]/td[1]',
    'machine' => '//tbody/tr[{row}]/td[2]',
    'pattern' => '//tbody/tr[{row}]/td[3]',
    'formatted' => '//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',
      'pattern' => '((((t+ig)+im)+if)+is)+jc',
      'formatted' => '(1) Mr John Peter Mark Doe Jnr., B.Sc., Ph.D. (2) JOAN SUE SMITH (3) Prince',
    ],
    2 => [
      'title' => t('Family'),
      'machine' => 'family',
      'pattern' => 'f',
      'formatted' => '(1) Doe (2) SMITH (3) <<empty>>',
      'edit link' => Url::fromRoute('entity.name_format.edit_form', [
        'name_format' => 'family',
      ])
        ->toString(),
      'delete link' => Url::fromRoute('entity.name_format.delete_form', [
        'name_format' => 'family',
      ])
        ->toString(),
    ],
    3 => [
      'title' => t('Full'),
      'machine' => 'full',
      'pattern' => '((((t+ig)+im)+if)+is)+jc',
      'formatted' => '(1) Mr John Peter Mark Doe Jnr., B.Sc., Ph.D. (2) JOAN SUE SMITH (3) Prince',
      'edit' => t('Edit'),
      'edit link' => Url::fromRoute('entity.name_format.edit_form', [
        'name_format' => 'full',
      ])
        ->toString(),
      'delete' => t('Delete'),
      'delete link' => Url::fromRoute('entity.name_format.delete_form', [
        'name_format' => 'full',
      ])
        ->toString(),
    ],
    4 => [
      'title' => t('Given'),
      'machine' => 'given',
      'pattern' => 'g',
      'formatted' => '(1) John (2) JOAN (3) Prince',
      'edit' => t('Edit'),
      'edit link' => Url::fromRoute('entity.name_format.edit_form', [
        'name_format' => 'given',
      ])
        ->toString(),
      'delete' => t('Delete'),
      'delete link' => Url::fromRoute('entity.name_format.delete_form', [
        'name_format' => 'given',
      ])
        ->toString(),
    ],
    5 => [
      'title' => t('Given Family'),
      'machine' => 'short_full',
      'pattern' => 'g+if',
      'formatted' => '(1) John Doe (2) JOAN SMITH (3) Prince',
      'edit link' => Url::fromRoute('entity.name_format.edit_form', [
        'name_format' => 'short_full',
      ])
        ->toString(),
      'delete link' => Url::fromRoute('entity.name_format.delete_form', [
        'name_format' => 'short_full',
      ])
        ->toString(),
    ],
    6 => [
      'title' => t('Title Family'),
      'machine' => 'formal',
      'pattern' => 't+if',
      'formatted' => '(1) Mr Doe (2) SMITH (3) <<empty>>',
      'edit link' => Url::fromRoute('entity.name_format.edit_form', [
        'name_format' => 'formal',
      ])
        ->toString(),
      'delete link' => Url::fromRoute('entity.name_format.delete_form', [
        'name_format' => 'formal',
      ])
        ->toString(),
    ],
  ];
  foreach ($all_values as $id => $row) {
    $this
      ->assertRow($row, $row_template, $id);
  }

  // Load the name settings form.
  $this
    ->drupalGet('admin/config/regional/name/settings');
  $default_values = [
    'name_settings[sep1]' => ' ',
    'name_settings[sep2]' => ', ',
    'name_settings[sep3]' => '',
  ];
  foreach ($default_values as $name => $value) {
    $this
      ->assertField($name, $value);
  }

  // ID example.
  $this
    ->assertFieldById('edit-name-settings-sep1', ' ', t('Sep 1 default value.'));
  $test_values = [
    'name_settings[sep1]' => '~',
    'name_settings[sep2]' => '^',
    'name_settings[sep3]' => '-',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/settings', $test_values, t('Save configuration'));
  $this
    ->assertText(t('The configuration options have been saved.'));
  foreach ($test_values as $name => $value) {
    $this
      ->assertField($name, $value);
  }

  // Delete all existing formats.
  $formats = NameFormat::loadMultiple();
  array_walk($formats, function (NameFormatInterface $format) {
    if (!$format
      ->isLocked()) {
      $format
        ->delete();
    }
  });
  $this
    ->drupalGet('admin/config/regional/name/add');
  $this
    ->assertRaw('Format string help', 'Testing the help fieldgroup');
  $values = [
    'label' => '',
    'id' => '',
    'pattern' => '',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/add', $values, t('Save format'));
  foreach ([
    t('Name'),
    t('Machine-readable name'),
    t('Format'),
  ] as $title) {
    $this
      ->assertText(t('@field field is required', [
      '@field' => $title,
    ]));
  }
  $values = [
    'label' => 'given',
    'id' => '1234567890abcdefghijklmnopqrstuvwxyz_',
    'pattern' => 'a',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/add', $values, t('Save format'));
  $this
    ->assertNoText(t('@field field is required', [
    '@field' => t('Format'),
  ]));
  $this
    ->assertNoText(t('@field field is required', [
    '@field' => t('Machine-readable name'),
  ]));
  $values = [
    'label' => 'given',
    'id' => '%&*(',
    'pattern' => 'a',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/add', $values, t('Save format'));
  $this
    ->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  $values = [
    'label' => 'default',
    'id' => 'default',
    'pattern' => 'a',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/add', $values, t('Save format'));
  $this
    ->assertText(t('The machine-readable name is already in use. It must be unique.'));
  $values = [
    'label' => 'Test',
    'id' => 'test',
    'pattern' => '\\a\\bc',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/add', $values, t('Save format'));
  $this
    ->assertText(t('Name format Test added.'));
  $row = [
    'title' => 'Test',
    'machine' => 'test',
    'pattern' => '\\a\\bc',
    'formatted' => '(1) abB.Sc., Ph.D. (2) ab (3) ab',
    'edit link' => Url::fromRoute('entity.name_format.edit_form', [
      'name_format' => 'test',
    ])
      ->toString(),
    'delete link' => Url::fromRoute('entity.name_format.delete_form', [
      'name_format' => 'test',
    ])
      ->toString(),
  ];
  $this
    ->assertRow($row, $row_template, 3);
  $values = [
    'label' => 'new name',
    'pattern' => 'f+g',
  ];
  $this
    ->drupalPostForm('admin/config/regional/name/manage/test', $values, t('Save format'));
  $this
    ->assertText(t('Name format new name has been updated.'));
  $row = [
    'label' => $values['label'],
    'id' => 'test',
    'pattern' => $values['pattern'],
  ];
  $this
    ->assertRow($row, $row_template, 3);
  $this
    ->drupalGet('admin/config/regional/name/manage/60');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('admin/config/regional/name/manage/60/delete');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('admin/config/regional/name/manage/test/delete');
  $this
    ->assertText(t('Are you sure you want to delete the custom format @title?', [
    '@title' => $values['label'],
  ]));
  $this
    ->drupalPostForm(NULL, [], t('Delete'));
  $this
    ->assertText(t('The name format @title has been deleted.', [
    '@title' => $values['label'],
  ]));
}