setAbsolute example

protected $defaultTheme = 'stark';

  /** * Tests the html head links. */
  public function testHtmlHeadLinks() {
    $node = $this->drupalCreateNode();

    $this->drupalGet($node->toUrl());

    $element = $this->assertSession()->elementExists('css', 'link[rel="canonical"]');
    $this->assertEquals($node->toUrl()->setAbsolute()->toString()$element->getAttribute('href'));

    $element = $this->assertSession()->elementExists('css', 'link[rel="shortlink"]');
    $this->assertEquals($node->toUrl('canonical', ['alias' => TRUE])->setAbsolute()->toString()$element->getAttribute('href'));
  }

  /** * Tests the Link header. */
  public function testLinkHeader() {
    $node = $this->drupalCreateNode();
    $this->drupalGet($node->toUrl());
    
return $media;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $file = File::load(1);
    $thumbnail = File::load(3);
    $author = User::load($this->entity->getOwnerId());
    $base_url = Url::fromUri('base:/jsonapi/media/camelids/' . $this->entity->uuid())->setAbsolute();
    $self_url = clone $base_url;
    $version_identifier = 'id:' . $this->entity->getRevisionId();
    $self_url = $self_url->setOption('query', ['resourceVersion' => $version_identifier]);
    $version_query_string = '?resourceVersion=' . urlencode($version_identifier);
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        

    $response = $this->request('GET', $url$request_options);
    $this->assertSame(200, $response->getStatusCode());

    $response = Json::decode((string) $response->getBody());
    $this->assertArrayNotHasKey('included', $response, 'JSON API response does not contain "included" taxonomy term as the latter is not published, i.e not accessible.');

    $omitted = $response['meta']['omitted']['links'];
    unset($omitted['help']);
    $omitted = reset($omitted);
    $expected_url = Url::fromUri('internal:/jsonapi/' . $term->getEntityTypeId() . '/' . $term->bundle() . '/' . $term->uuid());
    $expected_url->setAbsolute();
    $this->assertSame($expected_url->toString()$omitted['href'], 'Entity that is excluded due to access constraints is correctly reported in the "Omitted" section of the JSON API response.');

    $term->setPublished();
    $term->save();
    $response = $this->request('GET', $url$request_options);
    $this->assertSame(200, $response->getStatusCode());
    $this->assertEquals($term->uuid(), Json::decode((string) $response->getBody())['included'][0]['id'], 'JSON API response contains "included" taxonomy term as it became published, i.e accessible.');
  }

  /** * Tests that "virtual/missing" resources can exist for renamed fields. * * @see https://www.drupal.org/project/drupal/issues/3034786 * @see https://www.drupal.org/project/drupal/issues/3035544 */

  public function testUserEditPage() {

    $response = $this->userController->userEditPage();

    // Ensure the response is directed to the correct user edit page.     $edit_url = Url::fromRoute('entity.user.edit_form', [
      'user' => $this->user->id(),
    ])->setAbsolute()
      ->toString();
    $this->assertEquals($edit_url$response->getTargetUrl());

    $this->assertEquals(302, $response->getStatusCode());

  }

}
    // and we need to simply make it an absolute path by prepending the host.     if (str_starts_with($url_string, '/')) {
      $host = \Drupal::request()->getSchemeAndHttpHost();
      // @todo Views should expect and store a leading /.       // @see https://www.drupal.org/node/2423913       return $host . $url_string;
    }
    // Otherwise, this is an unprocessed path (e.g. node/123) and we need to run     // it through a Url object to allow outbound path processors to run (path     // aliases, language prefixes, etc).     else {
      return Url::fromUserInput('/' . $url_string)->setAbsolute()->toString();
    }
  }

}
'id' => 'test_rest',
      'label' => 'Test REST',
    ]);
    $view->save();
    return $view;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/view/view/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
/** @var \Drupal\file\FileInterface $duplicate */
    $duplicate = parent::createAnotherEntity($key);
    $duplicate->setFileUri("public://$key.txt");
    $duplicate->save();
    return $duplicate;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/file/file/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
'filename' => $filename,
        ],
      ],
      'cookies' => $this->getSessionCookies(),
      'http_errors' => FALSE,
    ];

    $this->assertFileDoesNotExist('temporary://' . $filename);
    // Use Guzzle's HTTP client directly so we can POST files without having to     // write them to disk. Not all filesystem support writing files with invalid     // UTF-8 filenames.     $response = $this->getHttpClient()->request('POST', Url::fromUri('base:file-test/upload')->setAbsolute()->toString()$data);

    $content = (string) $response->getBody();
    $this->htmlOutput($content);
    $error_text = new FormattableMarkup('The file %filename could not be uploaded because the name is invalid.', ['%filename' => $filename]);
    $this->assertStringContainsString((string) $error_text$content);
    $this->assertStringContainsString('Epic upload FAIL!', $content);
    $this->assertFileDoesNotExist('temporary://' . $filename);
  }

  /** * Tests the file_save_upload() function when the field is required. */


    $camelids->save();

    return $camelids;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/node_type/node_type/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
'status' => TRUE,
    ]);
    $view_display->save();

    return $view_display;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/entity_view_display/entity_view_display/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
$this->effectUuid = $camelids->addImageEffect($effect);

    $camelids->save();

    return $camelids;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/image_style/image_style/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
'id' => 'llama_set',
      'label' => 'Llama Set',
    ]);
    $set->save();
    return $set;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/shortcut_set/shortcut_set/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
return $entity_test;
  }

  /** * Tests using the file upload POST route; needs second request to "use" file. */
  public function testPostFileUpload() {
    $uri = Url::fromUri('base:' . static::$postUri);

    // DX: 405 when read-only mode is enabled.     $response = $this->fileRequest($uri$this->testFileData);
    $this->assertResourceErrorResponse(405, sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromUri('base:/admin/config/services/jsonapi')->setAbsolute()->toString(TRUE)->getGeneratedUrl())$uri$response);
    $this->assertSame(['GET']$response->getHeader('Allow'));

    $this->config('jsonapi.settings')->set('read_only', FALSE)->save(TRUE);

    // DX: 403 when unauthorized.     $response = $this->fileRequest($uri$this->testFileData);
    $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST')$uri$response);

    $this->setUpAuthorization('POST');

    // 404 when the field name is invalid.


  /** * Tests that the given path provides the correct alternate hreflang links. * * @param \Drupal\node\Entity\Node $node * The node to be tested. */
  protected function doTestAlternateHreflangLinks(Node $node) {
    $url = $node->toUrl();
    $languages = $this->container->get('language_manager')->getLanguages();
    $url->setAbsolute();
    $urls = [];
    $translations = [];
    foreach ($this->langcodes as $langcode) {
      $language_url = clone $url;
      $urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
      $translations[$langcode] = $node->getTranslation($langcode);
    }
    foreach ($this->langcodes as $langcode) {
      // Skip unpublished translations.       if ($translations[$langcode]->isPublished()) {
        $this->drupalGet($urls[$langcode]);
        
$supported_methods = $route->getMethods();
      assert(count($supported_methods) > 0, 'JSON:API routes always have a method specified.');
      $is_read_only_route = empty(array_diff($supported_methods$read_only_methods));
      if (!$is_read_only_route) {
        $collection->remove($name);
      }
    }
    if (count($collection)) {
      return $collection;
    }
    throw new MethodNotAllowedHttpException(array_intersect($all_supported_methods$read_only_methods)sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromRoute('jsonapi.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl()));
  }

}
Home | Imprint | This part of the site doesn't use cookies.