- <?php
- * @file
- * Tests for path.inc.
- */
-
- * Unit tests for the backdrop_match_path() function in path.inc.
- *
- * @see backdrop_match_path().
- */
- class BackdropMatchPathTestCase extends BackdropWebTestCase {
- protected $front;
-
- function setUp() {
-
- parent::setUp();
-
-
- $this->front = $this->randomName();
- config_set('system.core', 'site_frontpage', $this->front);
-
- $this->refreshVariables();
- }
-
-
- * Run through our test cases, making sure each one works as expected.
- */
- function testBackdropMatchPath() {
-
- $tests = $this->backdropMatchPathTests();
- foreach ($tests as $patterns => $cases) {
- foreach ($cases as $path => $expected_result) {
- $actual_result = backdrop_match_path($path, $patterns);
- $this->assertIdentical($actual_result, $expected_result, format_string('Tried matching the path <code>@path</code> to the pattern <pre>@patterns</pre> - expected @expected, got @actual.', array('@path' => $path, '@patterns' => $patterns, '@expected' => var_export($expected_result, TRUE), '@actual' => var_export($actual_result, TRUE))));
- }
- }
- }
-
-
- * Helper function for testBackdropMatchPath(): set up an array of test cases.
- *
- * @return
- * An array of test cases to cycle through.
- */
- private function backdropMatchPathTests() {
- return array(
-
- 'example/1' => array(
- 'example/1' => TRUE,
- 'example/2' => FALSE,
- 'test' => FALSE,
- ),
-
- 'example/*' => array(
- 'example/1' => TRUE,
- 'example/2' => TRUE,
- 'example/3/edit' => TRUE,
- 'example/' => TRUE,
- 'example' => FALSE,
- 'test' => FALSE,
- ),
-
- 'node/*/revisions/*' => array(
- 'node/1/revisions/3' => TRUE,
- 'node/345/revisions/test' => TRUE,
- 'node/23/edit' => FALSE,
- 'test' => FALSE,
- ),
-
- '<front>' => array(
- $this->front => TRUE,
- "$this->front/" => FALSE,
- "$this->front/edit" => FALSE,
- 'node' => FALSE,
- '' => FALSE,
- ),
-
- '<front>/*' => array(
- $this->front => FALSE,
- "$this->front/" => FALSE,
- "$this->front/edit" => FALSE,
- 'node/12' => FALSE,
- '' => FALSE,
- ),
-
- "node/*\nnode/*/edit" => array(
- 'node/1' => TRUE,
- 'node/view' => TRUE,
- 'node/32/edit' => TRUE,
- 'node/delete/edit' => TRUE,
- 'node/50/delete' => TRUE,
- 'test/example' => FALSE,
- ),
-
- "user/*\rexample/*" => array(
- 'user/1' => TRUE,
- 'example/1' => TRUE,
- 'user/1/example/1' => TRUE,
- 'user/example' => TRUE,
- 'test/example' => FALSE,
- 'user' => FALSE,
- 'example' => FALSE,
- ),
-
- "test\r\n<front>" => array(
- 'test' => TRUE,
- $this->front => TRUE,
- 'example' => FALSE,
- ),
-
- '[^/]+?/[0-9]' => array(
- 'test/1' => FALSE,
- '[^/]+?/[0-9]' => TRUE,
- ),
- );
- }
- }
-
- * Tests hook_url_alter functions.
- */
- class UrlAlterFunctionalTest extends BackdropWebTestCase {
- function setUp() {
- parent::setUp('path', 'url_alter_test');
- }
-
-
- * Test that URL altering works and that it occurs in the correct order.
- */
- function testUrlAlter() {
- $account = $this->backdropCreateUser(array('administer url aliases'));
-
- $account->path['auto'] = FALSE;
- $account->path['alias'] = '';
- $account->save();
- $this->backdropLogin($account);
-
- $uid = $account->uid;
- $name = $account->name;
-
-
- $this->assertUrlInboundAlter("user/$name", "user/$uid");
- $this->assertUrlOutboundAlter("user/$uid", "user/$name");
-
-
- $path = array('source' => "user/$uid/test1", 'alias' => 'alias/test1');
- path_save($path);
- $this->assertUrlInboundAlter('alias/test1', "user/$uid/test1");
- $this->assertUrlOutboundAlter("user/$uid/test1", 'alias/test1');
-
-
- $edit = array('source' => "user/$name/edit", 'alias' => 'alias/test2');
- $this->backdropPost('admin/config/urls/path/add', $edit, 'Save URL alias');
- $this->assertText(t('The alias has been saved.'));
-
-
- $this->assertUrlInboundAlter('alias/test2', "user/$uid/edit");
- $this->assertUrlOutboundAlter("user/$uid/edit", 'alias/test2');
-
-
- $uid++;
- $this->assertUrlInboundAlter("user/$uid", "user/$uid");
- $this->assertUrlOutboundAlter("user/$uid", "user/$uid");
- }
-
-
- * Test current_path() and request_path().
- */
- function testCurrentUrlRequestedPath() {
- $this->backdropGet('url-alter-test/bar');
- $this->assertRaw('request_path=url-alter-test/bar', 'request_path() returns the requested path.');
- $this->assertRaw('current_path=url-alter-test/foo', 'current_path() returns the internal path.');
- }
-
-
- * Tests that $_GET['q'] is initialized when the request path is empty.
- */
- function testGetQInitialized() {
- $this->backdropGet('');
- $this->assertText("\$_GET['q'] is non-empty with an empty request path.", "\$_GET['q'] is initialized with an empty request path.");
- }
-
-
- * Assert that an outbound path is altered to an expected value.
- *
- * @param $original
- * A string with the original path that is run through url().
- * @param $final
- * A string with the expected result after url().
- * @return
- * TRUE if $original was correctly altered to $final, FALSE otherwise.
- */
- protected function assertUrlOutboundAlter($original, $final) {
-
- $result = url($original);
- $base_path = base_path() . (config_get('system.core', 'clean_url') ? '' : '?q=');
- $result = substr($result, strlen($base_path));
- $this->assertIdentical($result, $final, format_string('Altered outbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
- }
-
-
- * Assert that a inbound path is altered to an expected value.
- *
- * @param $original
- * A string with the aliased or un-normal path that is run through
- * backdrop_get_normal_path().
- * @param $final
- * A string with the expected result after url().
- * @return
- * TRUE if $original was correctly altered to $final, FALSE otherwise.
- */
- protected function assertUrlInboundAlter($original, $final) {
-
- $result = backdrop_get_normal_path($original);
- $this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
- }
- }
-
- * Unit test for backdrop_lookup_path().
- */
- class PathLookupTest extends BackdropWebTestCase {
-
- * Test that backdrop_lookup_path() returns the correct path.
- */
- function testBackdropLookupPath() {
- $account = $this->backdropCreateUser();
- $uid = $account->uid;
- $name = $account->name;
-
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'foo',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'Basic alias lookup works.');
- $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'Basic source lookup works.');
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => "users/$name",
- 'langcode' => 'en',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
- $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'English source overrides language-neutral source.');
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'bar',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a language-neutral alias.');
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'LOL',
- 'langcode' => 'xx-lolspeak',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), "users/$name", 'English alias still returned after entering a LOLspeak alias.');
-
- $this->assertEqual(backdrop_lookup_path('alias', $path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to backdrop_lookup_path().');
-
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'users/my-new-path',
- 'langcode' => 'en',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), $path['alias'], 'Recently created English alias returned.');
- $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'Recently created English source returned.');
-
-
-
- db_delete('url_alias')
- ->condition('langcode', 'en')
- ->execute();
- backdrop_clear_path_cache();
- $this->assertEqual(backdrop_lookup_path('alias', $path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
-
-
-
- $account2 = $this->backdropCreateUser();
- $path = array(
- 'source' => 'user/' . $account2->uid,
- 'alias' => 'bar',
- );
- path_save($path);
- $this->assertEqual(backdrop_lookup_path('source', $path['alias']), $path['source'], 'Newer alias record is returned when comparing two LANGUAGE_NONE paths with the same alias.');
- }
- }
-
- * Tests the path_save() function.
- */
- class PathSaveTest extends BackdropWebTestCase {
- function setUp() {
-
- parent::setUp('path_test');
- path_test_reset();
- }
-
-
- * Tests that path_save() makes the original path available to modules.
- */
- function testBackdropSaveOriginalPath() {
- $account = $this->backdropCreateUser();
- $uid = $account->uid;
- $name = $account->name;
-
-
- $path = array(
- 'source' => "user/$uid",
- 'alias' => 'foo',
- );
- $path_original = $path;
- path_save($path);
-
-
- $path['alias'] = 'bar';
- path_save($path);
-
-
-
- $results = state_get('path_test_results', array());
- $this->assertIdentical($results['hook_path_update']['original']['alias'], $path_original['alias'], 'Old URL alias available to modules during hook_path_update.');
- $this->assertIdentical($results['hook_path_update']['original']['source'], $path_original['source'], 'Old URL alias available to modules during hook_path_update.');
- }
- }