toUrl example



  /** * Tests the "expand all items" feature. */
  public function testExpandAllItems() {
    $this->drupalLogin($this->adminUser);
    $menu = $this->addCustomMenu();
    $node = $this->drupalCreateNode(['type' => 'article']);

    // Create three menu items, none of which are expanded.     $parent = $this->addMenuLink('', $node->toUrl()->toString()$menu->id(), FALSE);
    $child_1 = $this->addMenuLink($parent->getPluginId()$node->toUrl()->toString()$menu->id(), FALSE);
    $child_2 = $this->addMenuLink($parent->getPluginId()$node->toUrl()->toString()$menu->id(), FALSE);

    // The menu will not automatically show all levels of depth.     $this->drupalGet('<front>');
    $this->assertSession()->linkExists($parent->getTitle());
    $this->assertSession()->linkNotExists($child_1->getTitle());
    $this->assertSession()->linkNotExists($child_2->getTitle());

    // Update the menu block to show all levels of depth as expanded.     $block_id = $this->blockPlacements[$menu->id()];
    
    // options_test_dynamic_values_callback().     $values = [
      'user_id' => mt_rand(1, 10),
      'name' => $this->randomMachineName(),
    ];
    $this->entity = EntityTestRev::create($values);
    $this->entity->save();
    $this->test = [
      'label' => $this->entity->label(),
      'uuid' => $this->entity->uuid(),
      'bundle' => $this->entity->bundle(),
      'uri' => $this->entity->toUrl()->toString(),
    ];
  }

}
// Check that at least one module implements hook_node_grants() as this test     // only tests this case.     // @see \node_test_node_grants()     $this->assertTrue(\Drupal::moduleHandler()->hasImplementations('node_grants'));

    // Create an unpublished node.     $referenced = $this->createNode(['status' => FALSE]);
    // Create a node referencing $referenced.     $node = $this->createNode(['ref' => $referenced]);

    // Check that the referenced entity link doesn't show on the host entity.     $this->drupalGet($node->toUrl());
    $this->assertSession()->linkNotExists($referenced->label());

    // Publish the referenced node.     $referenced->setPublished()->save();

    // Check that the referenced entity link shows on the host entity.     $this->getSession()->reload();
    $this->assertSession()->linkExists($referenced->label());
  }

}
// Set the formatter to link to the entity.     $this->display->setComponent($this->fieldName, [
      'type' => 'string',
      'settings' => [
        'link_to_entity' => TRUE,
      ],
    ]);
    $this->display->save();

    $this->renderEntityFields($entity$this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref($entity->toUrl()->toString());

    // $entity->toUrl('revision') falls back to the canonical URL if this is no     // revision.     $this->assertLinkByHref($entity->toUrl('revision')->toString());

    // Make the entity a new revision.     $old_revision_id = $entity->getRevisionId();
    $entity->setNewRevision(TRUE);
    $value2 = $this->randomMachineName();
    $entity->{$this->fieldName}->value = $value2;
    $entity->save();
    
'use editorial transition create_new_draft',
    ];
    $account = $this->drupalCreateUser($permissions);
    $this->drupalLogin($account);
    $webassert = $this->assertSession();

    // Check that admin is able to edit the node.     $nodes = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->loadByProperties(['title' => 'Deep mediterranean quiche']);
    $node = reset($nodes);
    $this->drupalGet($node->toUrl('edit-form'));
    $webassert->statusCodeEquals('200');

    $this->submitForm([], 'Preview');
    $webassert->statusCodeEquals('200');
    $this->assertSession()->elementsCount('css', 'h1', 1);
    $this->clickLink('Back to content editing');

    $this->submitForm([], "Save");
    $webassert->pageTextContains('Recipe Deep mediterranean quiche has been updated.');
  }

  
'label' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains("Saved the {$edit['label']} view mode.");

    // Test editing the view mode.     $this->drupalGet('admin/structure/display-modes/view/manage/entity_test.' . $edit['id']);

    // Test that the link templates added by field_ui_entity_type_build() are     // generating valid routes.     $view_mode = EntityViewMode::load('entity_test.' . $edit['id']);
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.collection')->toString()$view_mode->toUrl('collection')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.add_form', ['entity_type_id' => $view_mode->getTargetType()])->toString()$view_mode->toUrl('add-form')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.edit_form', ['entity_view_mode' => $view_mode->id()])->toString()$view_mode->toUrl('edit-form')->toString());
    $this->assertEquals(Url::fromRoute('entity.entity_view_mode.delete_form', ['entity_view_mode' => $view_mode->id()])->toString()$view_mode->toUrl('delete-form')->toString());

    // Test deleting the view mode.     $this->clickLink('Delete');
    $this->assertSession()->pageTextContains("Are you sure you want to delete the view mode {$edit['label']}?");
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains("The view mode {$edit['label']} has been deleted.");
  }

  
// Create an article.     $node = $this->drupalCreateNode([
      'type' => 'article',
      $this->fieldName => $term1->id(),
    ]);

    // Check that the term is displayed when the RSS feed is viewed.     $this->drupalGet('rss.xml');
    $test_element = sprintf(
      '<category %s>%s</category>',
      'domain="' . $term1->toUrl('canonical', ['absolute' => TRUE])->toString() . '"',
      $term1->getName()
    );
    $this->assertSession()->responseContains($test_element);

    // Test that the feed icon exists for the term.     $this->drupalGet("taxonomy/term/{$term1->id()}");
    $this->assertSession()->linkByHrefExists("taxonomy/term/{$term1->id()}/feed");

    // Test that the feed page exists for the term.     $this->drupalGet("taxonomy/term/{$term1->id()}/feed");
    $assert = $this->assertSession();
    
class ClaroPasswordConfirmWidgetTest extends PasswordConfirmWidgetTest {

  /** * {@inheritdoc} */
  protected $defaultTheme = 'claro';

  /** * Tests that password match message is invisible when widget is initialized. */
  public function testPasswordConfirmMessage() {
    $this->drupalGet($this->testUser->toUrl('edit-form'));
    $password_confirm_widget_selector = '.js-form-type-password-confirm.js-form-item-pass';
    $password_confirm_selector = '.js-form-item-pass-pass2';
    $password_confirm_widget = $this->assert->elementExists('css', $password_confirm_widget_selector);
    $password_confirm_item = $password_confirm_widget->find('css', $password_confirm_selector);

    // Password match message.     $this->assertTrue($password_confirm_item->has('css', 'input.js-password-confirm + [data-drupal-selector="password-confirm-message"]'));
    $this->assertFalse($password_confirm_item->find('css', 'input.js-password-confirm + [data-drupal-selector="password-confirm-message"]')->isVisible());
  }

  /** * {@inheritdoc} */
// Save the article node.     $title = $this->randomMachineName();
    $edit = [
      'title[0][value]' => $title,
    ];
    $edit['field_media_reference[0][target_id]'] = $mediaImage->getName();
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');

    // Validate the image being loaded with the media reference.     $this->assertSession()->responseContains('<a href="' . $mediaImage->toUrl('edit-form')->toString());

    // Retrieve the created node.     $node = $this->drupalGetNodeByTitle($title);
    $nid = $node->id();

    // Change the image thumbnail to point into the content node.     $this->changeMediaReferenceFieldLinkType('content');
    $node_storage->resetCache([$nid]);
    $this->drupalGet('node/' . $nid);

    // Validate image being loaded with the content on the link.
// Verify a title with HTML entities is properly escaped.     $node_title = 'This "cool" & "neat" article\'s title';
    $node = $this->drupalCreateNode([
      'title' => $node_title,
      'body' => [
        0 => [
          'value' => 'A paragraph',
          'format' => filter_default_format(),
        ],
      ],
    ]);
    $node_link = $node->toUrl()->setAbsolute()->toString();

    // Test the site name setting.     $site_name = $this->randomMachineName();
    $frontpage_url = Url::fromRoute('<front>')->setAbsolute()->toString();
    $this->config('system.site')->set('name', $site_name)->save();

    $this->drupalGet('test-feed-display.xml');
    $this->assertEquals($site_name$this->getSession()->getDriver()->getText('//channel/title'));
    $this->assertEquals($frontpage_url$this->getSession()->getDriver()->getText('//channel/link'));
    $this->assertEquals('Copyright 2019 Dries Buytaert', $this->getSession()->getDriver()->getText('//channel/copyright'));
    $this->assertEquals($node_title$this->getSession()->getDriver()->getText('//item/title'));
    

  public function testCanonicalAlternateTags() {
    /** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
    $languageManager = $this->container->get('language_manager');
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
    $entityTypeManager = $this->container->get('entity_type.manager');

    $definition = $entityTypeManager->getDefinition('entity_test_mul');
    $this->assertTrue($definition->hasLinkTemplate('canonical'), 'Canonical link template found for entity_test.');

    $entity = $this->createTranslatableEntity();
    $url_base = $entity->toUrl('canonical')
      ->setAbsolute();

    $langcodes_all = $this->langcodes;
    $langcodes_all[] = $languageManager
      ->getDefaultLanguage()
      ->getId();

    /** @var \Drupal\Core\Url[] $urls */
    $urls = array_map(
      function D$langcode) use ($url_base$languageManager) {
        $url = clone $url_base;
        
$this->createTranslatableEntity();

    $user = $this->drupalCreateUser(['view test entity']);
    $this->drupalLogin($user);
  }

  /** * Tests default with content language remaining same as interface language. */
  public function testDefaultConfiguration() {
    $translation = $this->entity;
    $this->drupalGet($translation->toUrl());
    $last = $this->container->get('state')->get('language_test.language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    $this->assertSame($last_content_language$last_interface_language);
    $this->assertSame($translation->language()->getId()$last_content_language);

    $translation = $this->entity->getTranslation('es');
    $this->drupalGet($translation->toUrl());
    $last = $this->container->get('state')->get('language_test.language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    
protected function doTestTranslationEdit() {
    $storage = $this->container->get('entity_type.manager')
      ->getStorage($this->entityTypeId);
    $storage->resetCache([$this->entityId]);
    $entity = $storage->load($this->entityId);
    $languages = $this->container->get('language_manager')->getLanguages();

    foreach ($this->langcodes as $langcode) {
      // We only want to test the title for non-english translations.       if ($langcode != 'en') {
        $options = ['language' => $languages[$langcode]];
        $url = $entity->toUrl('edit-form', $options);
        $this->drupalGet($url);
        $this->assertSession()->pageTextContains("Edit {$entity->bundle()} {$entity->getTranslation($langcode)->label()} [{$languages[$langcode]->getName()} translation]");
      }
    }
  }

}
/** * Tests to see if generator header is added. */
  public function testGeneratorHeaderAdded() {

    $node = $this->drupalCreateNode();

    [$version] = explode('.', \Drupal::VERSION, 2);
    $expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';

    // Check to see if the header is added when viewing an HTML page.     $this->drupalGet($node->toUrl());
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->assertSession()->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);

    // Check to see if the header is also added for a non-successful response.     $this->drupalGet('llama');
    $this->assertSession()->statusCodeEquals(404);
    $this->assertSession()->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->assertSession()->responseHeaderEquals('X-Generator', $expectedGeneratorHeader);

    // Enable cookie-based authentication for the entity:node REST resource.
        $field_definition = $entity->getFieldDefinition($field_name);
        if ($view_mode == 'teaser') {
          // Teaser view: display the number of comments that have been posted,           // or a link to add new comments if the user has permission, the           // entity is open to new comments, and there currently are none.           if ($this->currentUser->hasPermission('access comments')) {
            if (!empty($entity->get($field_name)->comment_count)) {
              $links['comment-comments'] = [
                'title' => $this->formatPlural($entity->get($field_name)->comment_count, '1 comment', '@count comments'),
                'attributes' => ['title' => $this->t('Jump to the first comment.')],
                'fragment' => 'comments',
                'url' => $entity->toUrl(),
              ];
              if ($this->moduleHandler->moduleExists('history')) {
                $links['comment-new-comments'] = [
                  'title' => '',
                  'url' => Url::fromRoute('<current>'),
                  'attributes' => [
                    'class' => 'hidden',
                    'title' => $this->t('Jump to the first new comment.'),
                    'data-history-node-last-comment-timestamp' => $entity->get($field_name)->last_comment_timestamp,
                    'data-history-node-field-name' => $field_name,
                  ],
                ];
Home | Imprint | This part of the site doesn't use cookies.