prepareRequestForGenerator example


  protected function rebuildContainer() {
    // Rebuild the kernel and bring it back to a fully bootstrapped state.     $this->container = $this->kernel->rebuildContainer();

    // Make sure the URL generator has a request object, otherwise calls to     // $this->drupalGet() will fail.     $this->prepareRequestForGenerator();
  }

  /** * Resets all data structures after having enabled new modules. * * This method is called by FunctionalTestSetupTrait::rebuildAll() after * enabling the requested modules. It must be called again when additional * modules are enabled later. * * @see \Drupal\Core\Test\FunctionalTestSetupTrait::rebuildAll() * @see \Drupal\Tests\BrowserTestBase::installDrupal() */
public function testImageStyleUrlForMissingSourceImage() {
    $non_existent_uri = 'public://foo.png';
    $generated_url = $this->style->buildUrl($non_existent_uri);
    $this->drupalGet($generated_url);
    $this->assertSession()->statusCodeEquals(404);
  }

  /** * Tests building an image style URL. */
  public function doImageStyleUrlAndPathTests($scheme$clean_url = TRUE, $extra_slash = FALSE, $langcode = FALSE) {
    $this->prepareRequestForGenerator($clean_url);

    // Make the default scheme neither "public" nor "private" to verify the     // functions work for other than the default scheme.     $this->config('system.file')->set('default_scheme', 'temporary')->save();

    // Create the directories for the styles.     $directory = $scheme . '://styles/' . $this->style->id();
    $status = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
    $this->assertNotFalse($status, 'Created the directory for the generated images for the test style.');

    // Override the language to build the URL for the correct language.
// Public files should not be served by Drupal, so their URLs should not be     // routed through Drupal, whereas private files should be served by Drupal,     // so they need to be. The difference is most apparent when $script_path     // is not empty (i.e., when not using clean URLs).     $clean_url_settings = [
      'clean' => '',
      'unclean' => 'index.php/',
    ];
    $public_directory_path = \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath();
    foreach ($clean_url_settings as $clean_url_setting => $script_path) {
      $clean_urls = $clean_url_setting == 'clean';
      $request = $this->prepareRequestForGenerator($clean_urls);
      $base_path = $request->getSchemeAndHttpHost() . $request->getBasePath();
      $this->checkUrl('public', '', $basename$base_path . '/' . $public_directory_path . '/' . $basename_encoded);
      $this->checkUrl('private', '', $basename$base_path . '/' . $script_path . 'system/files/' . $basename_encoded);
    }
    $this->assertEquals('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', $this->fileUrlGenerator->generateString('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', FALSE));
  }

  /** * Download a file from the URL generated by generateString(). * * Create a file with the specified scheme, directory and filename; check that * the URL generated by FileUrlGeneratorInterface::generateString() for the * specified file equals the specified URL; fetch the URL and then compare the * contents to the file. * * @param string $scheme * A scheme, e.g. "public". * @param string $directory * A directory, possibly "". * @param string $filename * A filename. * @param string $expected_url * The expected URL. */
->save();

    // Reset static caching.     $this->container->get('language_manager')->reset();

    // In case index.php is part of the URLs, we need to adapt the asserted     // URLs as well.     $index_php = str_contains(Url::fromRoute('<front>', []['absolute' => TRUE])->toString(), 'index.php');

    $request = Request::createFromGlobals();
    $server = $request->server->all();
    $request = $this->prepareRequestForGenerator(TRUE, ['HTTP_HOST' => $server['HTTP_HOST'] . ':88']);

    // Create an absolute French link.     $language = \Drupal::languageManager()->getLanguage('fr');
    $url = Url::fromRoute('<front>', [][
      'absolute' => TRUE,
      'language' => $language,
    ])->toString();

    $expected = ($index_php ? 'http://example.fr:88/index.php' : 'http://example.fr:88') . rtrim(base_path(), '/') . '/';

    $this->assertEquals($expected$url, 'The right port is used.');

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