/** * Test adding custom links with wrong/private/correct paths. */ public function testCustomLinks() { $language = $this->languageManager->getCurrentLanguage(); // Set a path alias for the node page. $alias = array('source' => 'system/files', 'alias' => 'public-files'); $this->aliasStorage->save('system/files', 'public-files', $language->getId()); $this->drupalGet('admin/config/search/xmlsitemap/custom'); $this->clickLink(t('Add custom link')); // Test an invalid path. $edit['loc'] = 'invalid-testing-path'; $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc']))); $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc'])); // Test a path not accessible to anonymous user. $edit['loc'] = 'admin/people/people'; $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc']))); $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc'])); // Test that the current page, which should not give a false positive for // $menu_item['access'] since the result has been cached already. $edit['loc'] = 'admin/config/search/xmlsitemap/custom/add'; $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc']))); $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc'])); }
/** * {@inheritdoc} */ public function execute() { $source = $this->getContextValue('source'); $alias = $this->getContextValue('alias'); $language = $this->getContextValue('language'); $langcode = isset($language) ? $language->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED; $this->aliasStorage->save($source, $alias, $langcode); }
/** * Tests the action execution when a language is specified. * * @covers ::execute */ public function testActionExecutionWithLanguage() { $language = $this->prophesize(LanguageInterface::class); $language->getId()->willReturn('en'); $this->aliasStorage->save('node/1', 'about', 'en')->shouldBeCalledTimes(1); $this->action->setContextValue('source', 'node/1')->setContextValue('alias', 'about')->setContextValue('language', $language->reveal()); $this->action->execute(); }
/** * Creates entity path alias. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that should get an alias. * @param string $alias * The alias to be created. */ protected function doExecute(EntityInterface $entity, $alias) { // We need to save the entity before we can get its internal path. if ($entity->isNew()) { $entity->save(); } $path = $entity->urlInfo()->getInternalPath(); $langcode = $entity->language()->getId(); $this->aliasStorage->save($path, $alias, $langcode); }
/** * Tests the action execution with a saved entity. * * @covers ::execute */ public function testActionExecutionWithSavedEntity() { // Test that the alias is only saved once. $this->aliasStorage->save('test/1', 'about', 'en')->shouldBeCalledTimes(1); $entity = $this->getMockEntity(); $entity->isNew()->willReturn(FALSE)->shouldBeCalledTimes(1); // Test that existing entities are not saved again. $entity->save()->shouldNotBeCalled(); $this->action->setContextValue('entity', $entity->reveal())->setContextValue('alias', 'about'); $this->action->execute(); }
/** * {@inheritdoc} */ public function execute() { $alias = $this->getContextValue('alias'); $entity = $this->getContextValue('entity'); // We need to save the entity before we can get its internal path. if ($entity->isNew()) { $entity->save(); } $path = $entity->urlInfo()->getInternalPath(); $langcode = $entity->language()->getId(); $this->aliasStorage->save($path, $alias, $langcode); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Remove unnecessary values. $form_state->cleanValues(); $pid = $form_state->getValue('pid', 0); $source = $form_state->getValue('source'); $alias = $form_state->getValue('alias'); // Language is only set if language.module is enabled, otherwise save for all // languages. $langcode = $form_state->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save($source, $alias, $langcode, $pid); drupal_set_message($this->t('The alias has been saved.')); $form_state->setRedirect('path.admin_overview'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { // Remove unnecessary values. form_state_values_clean($form_state); $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0; $source =& $form_state['values']['source']; $source = $this->aliasManager->getPathByAlias($source); $alias = $form_state['values']['alias']; // Language is only set if language.module is enabled, otherwise save for all // languages. $langcode = isset($form_state['values']['langcode']) ? $form_state['values']['langcode'] : LanguageInterface::LANGCODE_NOT_SPECIFIED; $this->aliasStorage->save($source, $alias, $langcode, $pid); drupal_set_message($this->t('The alias has been saved.')); $form_state['redirect_route'] = new Url('path.admin_overview'); }
protected function setUp() { parent::setUp(); $this->getDrupalContainer(); // Enfore full bootstrap $this->aliasStorage = $this->createAliasStorage(); $this->aliasStorage->save('duplicate-langcode', 'alias-fr', 'fr'); $this->aliasStorage->save('duplicate-langcode', 'alias-en', 'en'); $this->aliasStorage->save('duplicate-langcode', 'alias-und', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('duplicate-alias-1', 'duplicate-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('duplicate-alias-2', 'duplicate-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('normal-source', 'normal-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasManager = new AliasManager($this->aliasStorage); $GLOBALS['language'] = new Language(); }
/** * {@inheritdoc} */ public function save(array $path, $existing_alias = NULL, $op = NULL) { $config = $this->configFactory->get('pathauto.settings'); // Alert users if they are trying to create an alias that is the same as the // internal path. if ($path['source'] == $path['alias']) { $this->messenger->addMessage($this->t('Ignoring alias %alias because it is the same as the internal path.', array('%alias' => $path['alias']))); return NULL; } // Skip replacing the current alias with an identical alias. if (empty($existing_alias) || $existing_alias['alias'] != $path['alias']) { $path += array('pathauto' => TRUE, 'original' => $existing_alias, 'pid' => NULL); // If there is already an alias, respect some update actions. if (!empty($existing_alias)) { switch ($config->get('update_action')) { case PathautoManagerInterface::UPDATE_ACTION_NO_NEW: // Do not create the alias. return NULL; case PathautoManagerInterface::UPDATE_ACTION_LEAVE: // Create a new alias instead of overwriting the existing by leaving // $path['pid'] empty. break; case PathautoManagerInterface::UPDATE_ACTION_DELETE: // The delete actions should overwrite the existing alias. $path['pid'] = $existing_alias['pid']; break; } } // Save the path array. $this->aliasStorage->save($path['source'], $path['alias'], $path['language'], $path['pid']); if (!empty($existing_alias['pid'])) { $this->messenger->addMessage($this->t('Created new alias %alias for %source, replacing %old_alias.', array('%alias' => $path['alias'], '%source' => $path['source'], '%old_alias' => $existing_alias['alias']))); } else { $this->messenger->addMessage($this->t('Created new alias %alias for %source.', array('%alias' => $path['alias'], '%source' => $path['source']))); } return $path; } }
protected function setUp() { // We need to bootstrap at least Drupal configuration so that // variable_get() is defined parent::setUp(); $this->getDrupalContainer(); if (!defined('LANGUAGE_NONE')) { define('LANGUAGE_NONE', 'und'); } if (!defined('LANGUAGE_NEGOTIATION_DEFAULT')) { define('LANGUAGE_NEGOTIATION_DEFAULT', 'language-default'); } // Avoid variable_get() and variable_set() calls $GLOBALS['conf']['path_alias_whitelist'] = false; $this->aliasStorage = new ArrayAliasStorage(); $this->aliasStorage->save('duplicate-langcode', 'alias-fr', 'fr'); $this->aliasStorage->save('duplicate-langcode', 'alias-en', 'en'); $this->aliasStorage->save('duplicate-langcode', 'alias-und', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('duplicate-alias-1', 'duplicate-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('duplicate-alias-2', 'duplicate-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasStorage->save('normal-source', 'normal-alias', LanguageInterface::LANGCODE_NOT_SPECIFIED); $this->aliasManager = new AliasManager($this->aliasStorage); $GLOBALS['language'] = new Language(); }
/** * Creates an alias for an existing path. * * @param string $source * The existing path that should be aliased. * @param string $alias * The alias path that should be created. * @param \Drupal\Core\Language\LanguageInterface $language * (optional) The language. */ protected function doExecute($source, $alias, LanguageInterface $language = NULL) { $langcode = isset($language) ? $language->getId() : LanguageInterface::LANGCODE_NOT_SPECIFIED; $this->aliasStorage->save($source, $alias, $langcode); }