toUriString example

public function testGenerateURI($filepath$expected) {
    // Disable file_test.module's hook_file_url_alter() implementation.     \Drupal::state()->set('file_test.hook_file_url_alter', NULL);

    // Create a mock Request for transformRelative().     $request = Request::create($GLOBALS['base_url']);
    $this->container->get('request_stack')->push($request);
    \Drupal::setContainer($this->container);

    // No schema file.     $url = $this->fileUrlGenerator->generate($filepath);
    $this->assertEquals($expected$url->toUriString());
  }

  /** * @covers ::generate */
  public function testGenerateURIWithSchema() {
    // Disable file_test.module's hook_file_url_alter() implementation.     \Drupal::state()->set('file_test.hook_file_url_alter', NULL);

    // Create a mock Request for transformRelative().     $request = Request::create($GLOBALS['base_url']);
    
// @todo Views should expect and store a leading /. See         // https://www.drupal.org/node/2423913.         $alter['url'] = CoreUrl::fromUserInput('/' . ltrim($path, '/'));
      }
      else {
        $alter['url'] = CoreUrl::fromUri($path);
      }
    }

    $options = $alter['url']->getOptions() + $options;

    $path = $alter['url']->setOptions($options)->toUriString();

    if (!empty($alter['path_case']) && $alter['path_case'] != 'none' && !$alter['url']->isRouted()) {
      $path = str_replace($alter['path']$this->caseTransform($alter['path']$this->options['alter']['path_case'])$path);
    }

    if (!empty($alter['replace_spaces'])) {
      $path = str_replace(' ', '-', $path);
    }

    // Parse the URL and move any query and fragment parameters out of the path.     $url = UrlHelper::parse($path);

    


  /** * Tests the toUriString() method with entity: URIs. * * @covers ::toUriString * * @dataProvider providerTestToUriStringForEntity */
  public function testToUriStringForEntity($uri$options$uri_string) {
    $url = Url::fromUri($uri$options);
    $this->assertSame($url->toUriString()$uri_string);
  }

  /** * Data provider for testing string entity URIs. */
  public function providerTestToUriStringForEntity() {
    return [
      ['entity:test_entity/1', [], 'route:entity.test_entity.canonical;test_entity=1'],
      ['entity:test_entity/1', ['fragment' => 'top', 'query' => ['page' => '2']], 'route:entity.test_entity.canonical;test_entity=1?page=2#top'],
      ['entity:test_entity/1?page=2#top', [], 'route:entity.test_entity.canonical;test_entity=1?page=2#top'],
    ];
  }
$file = File::create([
      'uri' => 'dummy-external-readonly://file-query-string?foo=bar',
      'filename' => 'file-query-string',
    ]);
    $file->save();
    $file_link = [
      '#theme' => 'file_link',
      '#file' => $file,
    ];

    $output = \Drupal::service('renderer')->renderRoot($file_link);
    $this->assertStringContainsString($this->fileUrlGenerator->generate('dummy-external-readonly://file-query-string?foo=bar')->toUriString()$output);
  }

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