You are here

public function ContactStorageTest::testUrlAlias in Contact Storage 8

Tests the url alias creation feature.

File

tests/src/Functional/ContactStorageTest.php, line 331

Class

ContactStorageTest
Tests storing contact messages and viewing them through UI.

Namespace

Drupal\Tests\contact_storage\Functional

Code

public function testUrlAlias() {

  // Add a second language to make sure aliases work with any language.
  $language = ConfigurableLanguage::createFromLangcode('de');
  $language
    ->save();

  // Set the second language as default.
  $this
    ->config('system.site')
    ->set('default_langcode', $language
    ->getId())
    ->save();
  $this
    ->rebuildContainer();
  $mail = 'simpletest@example.com';

  // Test for alias without slash.
  $this
    ->addContactForm('form_alias_1', 'contactForm', $mail, FALSE, [
    'contact_storage_url_alias' => 'form51',
  ]);
  $this
    ->assertText('The alias path has to start with a slash.');
  $this
    ->drupalGet('form51');
  $this
    ->assertResponse(404);

  // Test for correct alias. Verify that we land on the correct contact form.
  $this
    ->addContactForm('form_alias_2', 'contactForm', $mail, FALSE, [
    'contact_storage_url_alias' => '/form51',
  ]);
  $this
    ->assertText('Contact form contactForm has been added.');
  $this
    ->drupalGet('form51');
  $this
    ->assertResponse(200);
  $this
    ->assertText('contactForm');

  // Edit the contact form without changing anything. Verify that the existing
  // alias continues to work.
  $this
    ->drupalPostForm('admin/structure/contact/manage/form_alias_2', [], 'Save');
  $this
    ->assertText('Contact form contactForm has been updated.');
  $this
    ->drupalGet('form51');
  $this
    ->assertResponse(200);

  // Edit the contact form by changing the alias. Verify that the new alias
  // is generated and the old one removed.
  $this
    ->drupalPostForm('admin/structure/contact/manage/form_alias_2', [
    'contact_storage_url_alias' => '/form52',
  ], 'Save');
  $this
    ->assertText('Contact form contactForm has been updated.');
  $this
    ->drupalGet('form51');
  $this
    ->assertResponse(404);
  $this
    ->drupalGet('form52');
  $this
    ->assertResponse(200);
  $this
    ->assertText('contactForm');

  // Edit the contact form by removing the alias. Verify that is is deleted.
  $this
    ->drupalPostForm('admin/structure/contact/manage/form_alias_2', [
    'contact_storage_url_alias' => '',
  ], 'Save');
  $this
    ->assertText('Contact form contactForm has been updated.');
  $this
    ->drupalGet('form52');
  $this
    ->assertResponse(404);

  // Add an alias back and delete the contact form. Verify that the alias is
  // deleted along with the contact form.
  $this
    ->drupalPostForm('admin/structure/contact/manage/form_alias_2', [
    'contact_storage_url_alias' => '/form52',
  ], 'Save');
  $this
    ->assertText('Contact form contactForm has been updated.');
  $this
    ->drupalGet('form52');
  $this
    ->assertResponse(200);
  $this
    ->assertText('contactForm');
  $this
    ->drupalPostForm('admin/structure/contact/manage/form_alias_2/delete', [], 'Delete');
  $alias = $this
    ->loadPathAliasByConditions([
    'path' => '/contact/form_alias_2',
  ]);
  $this
    ->assertNull($alias);
}