annotate core/modules/path/tests/src/Functional/PathTaxonomyTermTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\path\Functional;
Chris@0 4
Chris@0 5 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests URL aliases for taxonomy terms.
Chris@0 9 *
Chris@0 10 * @group path
Chris@0 11 */
Chris@0 12 class PathTaxonomyTermTest extends PathTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Modules to enable.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 public static $modules = ['taxonomy'];
Chris@0 20
Chris@0 21 protected function setUp() {
Chris@0 22 parent::setUp();
Chris@0 23
Chris@0 24 // Create a Tags vocabulary for the Article node type.
Chris@0 25 $vocabulary = Vocabulary::create([
Chris@0 26 'name' => t('Tags'),
Chris@0 27 'vid' => 'tags',
Chris@0 28 ]);
Chris@0 29 $vocabulary->save();
Chris@0 30
Chris@0 31 // Create and log in user.
Chris@0 32 $web_user = $this->drupalCreateUser(['administer url aliases', 'administer taxonomy', 'access administration pages']);
Chris@0 33 $this->drupalLogin($web_user);
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Tests alias functionality through the admin interfaces.
Chris@0 38 */
Chris@0 39 public function testTermAlias() {
Chris@0 40 // Create a term in the default 'Tags' vocabulary with URL alias.
Chris@0 41 $vocabulary = Vocabulary::load('tags');
Chris@0 42 $description = $this->randomMachineName();
Chris@0 43 $edit = [
Chris@0 44 'name[0][value]' => $this->randomMachineName(),
Chris@0 45 'description[0][value]' => $description,
Chris@0 46 'path[0][alias]' => '/' . $this->randomMachineName(),
Chris@0 47 ];
Chris@0 48 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save'));
Chris@0 49 $tid = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1", [':name' => $edit['name[0][value]']])->fetchField();
Chris@0 50
Chris@0 51 // Confirm that the alias works.
Chris@0 52 $this->drupalGet($edit['path[0][alias]']);
Chris@0 53 $this->assertText($description, 'Term can be accessed on URL alias.');
Chris@0 54
Chris@0 55 // Confirm the 'canonical' and 'shortlink' URLs.
Chris@0 56 $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
Chris@0 57 $this->assertTrue(!empty($elements), 'Term page contains canonical link URL.');
Chris@0 58 $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]");
Chris@0 59 $this->assertTrue(!empty($elements), 'Term page contains shortlink URL.');
Chris@0 60
Chris@0 61 // Change the term's URL alias.
Chris@0 62 $edit2 = [];
Chris@0 63 $edit2['path[0][alias]'] = '/' . $this->randomMachineName();
Chris@0 64 $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
Chris@0 65
Chris@0 66 // Confirm that the changed alias works.
Chris@0 67 $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
Chris@0 68 $this->assertText($description, 'Term can be accessed on changed URL alias.');
Chris@0 69
Chris@0 70 // Confirm that the old alias no longer works.
Chris@0 71 $this->drupalGet(trim($edit['path[0][alias]'], '/'));
Chris@0 72 $this->assertNoText($description, 'Old URL alias has been removed after altering.');
Chris@0 73 $this->assertResponse(404, 'Old URL alias returns 404.');
Chris@0 74
Chris@0 75 // Remove the term's URL alias.
Chris@0 76 $edit3 = [];
Chris@0 77 $edit3['path[0][alias]'] = '';
Chris@0 78 $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
Chris@0 79
Chris@0 80 // Confirm that the alias no longer works.
Chris@0 81 $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
Chris@0 82 $this->assertNoText($description, 'Old URL alias has been removed after altering.');
Chris@0 83 $this->assertResponse(404, 'Old URL alias returns 404.');
Chris@0 84 }
Chris@0 85
Chris@0 86 }