createPathAlias example

->setComponent($field_name[
        'type' => 'link',
      ])
      ->save();

    // Display creation form.     $this->drupalGet('entity_test/add');
    $this->assertSession()->fieldValueEquals("{$field_name}[0][uri]", '');
    $this->assertSession()->responseContains('placeholder="http://example.com"');

    // Create a path alias.     $this->createPathAlias('/admin', '/a/path/alias');

    // Create a node to test the link widget.     $node = $this->drupalCreateNode();

    $restricted_node = $this->drupalCreateNode(['status' => NodeInterface::NOT_PUBLISHED]);

    // Define some valid URLs (keys are the entered values, values are the     // strings displayed to the user).     $valid_external_entries = [
      'http://www.example.com/' => 'http://www.example.com/',
      // Strings within parenthesis without leading space char.
'title' => $node_title,
      'body' => [
        0 => [
          'value' => 'A paragraph',
          'format' => filter_default_format(),
        ],
      ],
    ]);

    // Create an alias to verify that outbound processing runs on the link and     // ensure that the node actually contains that.     $this->createPathAlias('/node/' . $node->id(), '/the-article-alias');

    $node_link = $node->toUrl()->setAbsolute()->toString();
    $this->assertStringContainsString('/the-article-alias', $node_link);

    $this->drupalGet('test-feed-display-fields.xml');
    $this->assertEquals($node_title$this->getSession()->getDriver()->getText('//item/title'));
    $this->assertEquals($node_link$this->getSession()->getDriver()->getText('//item/link'));
    // Verify HTML is properly escaped in the description field.     $this->assertSession()->responseContains('<p>A paragraph</p>');

    // Change the display to use the nid field, which is rewriting output as
'bar' => $this->randomMachineName(),
    ];
    $storage = new DatabaseStorage(Database::getConnection(), 'config');
    $storage->write('test_config', $this->data);

    // Create user account with some potential syntax issues.     // cspell:disable-next-line     $account = User::create(['mail' => 'q\'uote$dollar@example.com', 'name' => '$dollar']);
    $account->save();

    // Create a path alias.     $this->createPathAlias('/user/' . $account->id(), '/user/example');

    // Create a cache table (this will create 'cache_discovery').     \Drupal::cache('discovery')->set('test', $this->data);

    // These are all the tables that should now be in place.     $this->tables = [
      'block_content',
      'block_content_field_data',
      'block_content_field_revision',
      'block_content_revision',
      'cachetags',
      
// The alias whitelist expects that the menu path roots are set by a     // menu router rebuild.     \Drupal::state()->set('router.path_roots', ['user', 'admin']);

    $this->installEntitySchema('path_alias');
  }

  /** * @covers ::lookupBySystemPath */
  public function testLookupBySystemPath() {
    $this->createPathAlias('/test-source-Case', '/test-alias');

    $path_alias_repository = $this->container->get('path_alias.repository');
    $this->assertEquals('/test-alias', $path_alias_repository->lookupBySystemPath('/test-source-Case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
    $this->assertEquals('/test-alias', $path_alias_repository->lookupBySystemPath('/test-source-case', LanguageInterface::LANGCODE_NOT_SPECIFIED)['alias']);
  }

  /** * @covers ::lookupByAlias */
  public function testLookupByAlias() {
    $this->createPathAlias('/test-source', '/test-alias-Case');

    
// A path with placeholders.     $path = '/path/1/one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:[language]=en:/path/1/one:');
    $this->assertEquals('/path/1/one', $cache->data['path']);
    $this->assertEquals([]$cache->data['query']);
    $this->assertCount(2, $cache->data['routes']);

    // A path with a path alias.     $this->createPathAlias('/path/add/one', '/path/add-one');
    /** @var \Drupal\path_alias\AliasManagerInterface $alias_manager */
    $alias_manager = \Drupal::service('path_alias.manager');
    $alias_manager->cacheClear();

    $path = '/path/add-one';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:[language]=en:/path/add-one:');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals([]$cache->data['query']);
    
$this->drupalLogin($account);

    $uid = $account->id();
    $name = $account->getAccountName();

    // Test a single altered path.     $this->drupalGet("user/$name");
    $this->assertSession()->statusCodeEquals(200);
    $this->assertUrlOutboundAlter("/user/$uid", "/user/$name");

    // Test that a path always uses its alias.     $this->createPathAlias("/user/$uid/test1", '/alias/test1');
    $this->rebuildContainer();
    $this->assertUrlInboundAlter('/alias/test1', "/user/$uid/test1");
    $this->assertUrlOutboundAlter("/user/$uid/test1", '/alias/test1');

    // Test adding an alias via the UI.     $edit = ['path[0][value]' => "/user/$uid/edit", 'alias[0][value]' => '/alias/test2'];
    $this->drupalGet('admin/config/search/path/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('The alias has been saved.');
    $this->drupalGet('alias/test2');
    $this->assertSession()->statusCodeEquals(200);
    
parent::register($container);

    $definition = $container->getDefinition('path_alias.path_processor');
    $definition
      ->addTag('path_processor_inbound', ['priority' => 100]);
  }

  /** * Tests the path aliasing changing. */
  public function testPathAliasChange() {
    $path_alias = $this->createPathAlias('/test-page', '/my-blog');
    $menu_link_content = MenuLinkContent::create([
      'title' => 'Menu title',
      'link' => ['uri' => 'internal:/my-blog'],
      'menu_name' => 'tools',
    ]);
    $menu_link_content->save();

    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
    $this->assertEquals('test_page_test.test_page', $tree[$menu_link_content->getPluginId()]->link->getPluginDefinition()['route_name']);

    // Saving an alias should clear the alias manager cache.
    if ($container->hasDefinition('path_alias.path_processor')) {
      $definition = $container->getDefinition('path_alias.path_processor');
      $definition->addTag('path_processor_inbound', ['priority' => 100])->addTag('path_processor_outbound', ['priority' => 300]);
    }
  }

  /** * Tests the content negotiation aspect of routing. */
  public function testContentRouting() {
    // Alias with extension pointing to no extension/constant content-type.     $this->createPathAlias('/conneg/html', '/alias.html');

    // Alias with extension pointing to dynamic extension/linked content-type.     $this->createPathAlias('/conneg/html?_format=json', '/alias.json');

    $tests = [
      // ['path', 'accept', 'content-type'],
      // Extension is part of the route path. Constant Content-type.       ['conneg/simple.json', '', 'application/json'],
      ['conneg/simple.json', 'application/xml', 'application/json'],
      ['conneg/simple.json', 'application/json', 'application/json'],
      


  public function testAliasToEncoded() {
    $route_paths = [
      'path_encoded_test.colon' => '/hi/llamma:party',
      'path_encoded_test.atsign' => '/bloggy/@Dries',
      'path_encoded_test.parens' => '/cat(box)',
    ];
    $aliases = [];
    foreach ($route_paths as $route_name => $path) {
      $aliases[$route_name] = $this->randomMachineName();
      $this->createPathAlias($path, '/' . $aliases[$route_name]);
    }
    foreach ($route_paths as $route_name => $path) {
      // The alias may be only a suffix of the generated path when the test is       // run with Drupal installed in a subdirectory.       $this->assertMatchesRegularExpression('@/' . rawurlencode($aliases[$route_name]) . '$@', Url::fromRoute($route_name)->toString());
      $this->drupalGet(Url::fromRoute($route_name));
      $this->assertSession()->pageTextContains('PathEncodedTestController works');
    }
  }

}
    $this->drupalGet($english_path);
    $this->assertSession()->pageTextContains($node->label());

    // Confirm custom language path alias works.     $this->drupalGet($prefix . '/' . $custom_language_path);
    $this->assertSession()->pageTextContains($node->label());

    // Create a custom path.     $custom_path = $this->randomMachineName(8);

    // Check priority of language for alias by source path.     $path_alias = $this->createPathAlias('/node/' . $node->id(), '/' . $custom_path, LanguageInterface::LANGCODE_NOT_SPECIFIED);
    $lookup_path = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $node->id(), 'en');
    $this->assertEquals('/' . $english_path$lookup_path, 'English language alias has priority.');
    // Same check for language 'xx'.     $lookup_path = $this->container->get('path_alias.manager')->getAliasByPath('/node/' . $node->id()$prefix);
    $this->assertEquals('/' . $custom_language_path$lookup_path, 'Custom language alias has priority.');
    $path_alias->delete();

    // Create language nodes to check priority of aliases.     $first_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => 'en']);
    $second_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);

    
$this->drupalPlaceBlock('page_title_block');
  }

  /** * Tests that creating a shortcut works properly. */
  public function testShortcutLinkAdd() {
    $set = $this->set;

    // Create an alias for the node so we can test aliases.     $path_alias = $this->createPathAlias('/node/' . $this->node->id(), '/' . $this->randomMachineName(8));

    // Create some paths to test.     $test_cases = [
      '/',
      '/admin',
      '/admin/config/system/site-information',
      '/node/' . $this->node->id() . '/edit',
      $path_alias->getAlias(),
      '/router_test/test2',
      '/router_test/test3/value',
    ];

    
/** * Tests aliases when the negotiated language is not in the path. */
  public function testAliases() {
    // Switch to French and try to access the now inaccessible block.     $this->drupalGet('');

    // Create an alias for user/UID just for en, make sure that this is a 404     // on the french page exist in english, no matter which language is     // checked first. Create the alias after visiting frontpage to make sure     // there is no existing cache entry for this that affects the tests.     $this->createPathAlias('/user/' . $this->adminUser->id(), '/user-page', 'en');

    $this->clickLink('French');
    $this->drupalGet('user-page');
    $this->assertSession()->statusCodeEquals(404);

    // Switch to english, make sure it works now.     $this->clickLink('English');
    $this->drupalGet('user-page');
    $this->assertSession()->statusCodeEquals(200);

    // Clear cache and repeat the check, this time with english first.
$pt_br_translation = $node->addTranslation('pt-br');
    $pt_br_translation->set('title', 'pt-br');
    $pt_br_translation->set('body', [['value' => 'Algo em Português']]);
    $pt_br_translation->save();

    // First, check everything with raw node paths (e.g. node/1).     $this->checkFeedResults('raw-node-path', $node);

    // Now, create path aliases for each translation.     $node_path = '/node/' . $node->id();
    $this->createPathAlias($node_path, "$node_path/en-alias");
    $this->createPathAlias($node_path, "$node_path/es-alias", 'es');
    $this->createPathAlias($node_path, "$node_path/pt-br-alias", 'pt-br');
    // Save the node again, to clear the cache on the feed.     $node->save();
    // Assert that all the results are correct using path aliases.     $this->checkFeedResults('path-alias', $node);
  }

  /** * Checks the feed results for the given style of node links. * * @param string $link_style * What style of links do we expect? Either 'raw-node-path' or 'path-alias'. * Only used for human-readable assert failure messages. * @param \Drupal\node\Entity\Node $node * The node entity that's been created. */
Home | Imprint | This part of the site doesn't use cookies.