View source
<?php
namespace Drupal\system\Tests\Path;
use Drupal\simpletest\WebTestBase;
class UrlAlterFunctionalTest extends WebTestBase {
public static $modules = array(
'path',
'forum',
'url_alter_test',
);
function testUrlAlter() {
$account = $this
->drupalCreateUser(array(
'administer url aliases',
));
$this
->drupalLogin($account);
$uid = $account
->id();
$name = $account
->getUsername();
$this
->drupalGet("user/{$name}");
$this
->assertResponse('200', 'The user/username path gets resolved correctly');
$this
->assertUrlOutboundAlter("/user/{$uid}", "/user/{$name}");
$path = array(
'source' => "/user/{$uid}/test1",
'alias' => '/alias/test1',
);
$this->container
->get('path.alias_storage')
->save($path['source'], $path['alias']);
$this
->rebuildContainer();
$this
->assertUrlInboundAlter('/alias/test1', "/user/{$uid}/test1");
$this
->assertUrlOutboundAlter("/user/{$uid}/test1", '/alias/test1');
$edit = array(
'source' => "/user/{$uid}/edit",
'alias' => '/alias/test2',
);
$this
->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
$this
->assertText(t('The alias has been saved.'));
$this
->drupalGet('alias/test2');
$this
->assertResponse('200', 'The path alias gets resolved correctly');
$this
->assertUrlOutboundAlter("/user/{$uid}/edit", '/alias/test2');
$uid++;
$this
->assertUrlOutboundAlter("/user/{$uid}", "/user/{$uid}");
$this
->drupalGet('community');
$this
->assertText('General discussion', 'The community path gets resolved correctly');
$this
->assertUrlOutboundAlter('/forum', '/community');
$forum_vid = $this
->config('forum.settings')
->get('vocabulary');
$term_name = $this
->randomMachineName();
$term = entity_create('taxonomy_term', array(
'name' => $term_name,
'vid' => $forum_vid,
));
$term
->save();
$this
->drupalGet("community/" . $term
->id());
$this
->assertText($term_name, 'The community/{tid} path gets resolved correctly');
$this
->assertUrlOutboundAlter("/forum/" . $term
->id(), "/community/" . $term
->id());
}
protected function assertUrlOutboundAlter($original, $final) {
$result = $this->container
->get('path_processor_manager')
->processOutbound($original);
return $this
->assertIdentical($result, $final, format_string('Altered outbound URL %original, expected %final, and got %result.', array(
'%original' => $original,
'%final' => $final,
'%result' => $result,
)));
}
protected function assertUrlInboundAlter($original, $final) {
$result = $this->container
->get('path.alias_manager')
->getPathByAlias($original);
return $this
->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array(
'%original' => $original,
'%final' => $final,
'%result' => $result,
)));
}
}