Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / image / src / Tests / ImageStylesPathAndUrlTest.php
index baa4e7fd533d569224698e23292bdcb60da0f654..eef166c95169e04d1927eb40713549959d2e7c61 100644 (file)
@@ -3,6 +3,7 @@
 namespace Drupal\image\Tests;
 
 use Drupal\image\Entity\ImageStyle;
+use Drupal\language\Entity\ConfigurableLanguage;
 use Drupal\simpletest\WebTestBase;
 
 /**
@@ -17,18 +18,29 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
    *
    * @var array
    */
-  public static $modules = ['image', 'image_module_test'];
+  public static $modules = ['image', 'image_module_test', 'language'];
 
   /**
+   * The image style.
+   *
    * @var \Drupal\image\ImageStyleInterface
    */
   protected $style;
 
+  /**
+   * {@inheritdoc}
+   */
   protected function setUp() {
     parent::setUp();
 
-    $this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
+    $this->style = ImageStyle::create([
+      'name' => 'style_foo',
+      'label' => $this->randomString(),
+    ]);
     $this->style->save();
+
+    // Create a new language.
+    ConfigurableLanguage::createFromLangcode('fr')->save();
   }
 
   /**
@@ -73,6 +85,20 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
     $this->doImageStyleUrlAndPathTests('private', FALSE);
   }
 
+  /**
+   * Tests an image style URL with the "public://" schema and language prefix.
+   */
+  public function testImageStyleUrlAndPathPublicLanguage() {
+    $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE, 'fr');
+  }
+
+  /**
+   * Tests an image style URL with the "private://" schema and language prefix.
+   */
+  public function testImageStyleUrlAndPathPrivateLanguage() {
+    $this->doImageStyleUrlAndPathTests('private', TRUE, TRUE, 'fr');
+  }
+
   /**
    * Tests an image style URL with a file URL that has an extra slash in it.
    */
@@ -93,7 +119,7 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
   /**
    * Tests building an image style URL.
    */
-  public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE) {
+  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
@@ -105,6 +131,13 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
     $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
     $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');
 
+    // Override the language to build the URL for the correct language.
+    if ($langcode) {
+      $language_manager = \Drupal::service('language_manager');
+      $language = $language_manager->getLanguage($langcode);
+      $language_manager->setConfigOverrideLanguage($language);
+    }
+
     // Create a working copy of the file.
     $files = $this->drupalGetTestFiles('image');
     $file = array_shift($files);
@@ -119,6 +152,11 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
     $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
     $generate_url = $this->style->buildUrl($original_uri, $clean_url);
 
+    // Make sure that language prefix is never added to the image style URL.
+    if ($langcode) {
+      $this->assertTrue(strpos($generate_url, "/$langcode/") === FALSE, 'Langcode was not found in the image style URL.');
+    }
+
     // Ensure that the tests still pass when the file is generated by accessing
     // a poorly constructed (but still valid) file URL that has an extra slash
     // in it.
@@ -157,7 +195,8 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
     $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.');
 
     // Check that we did not download the original file.
-    $original_image = $this->container->get('image.factory')->get($original_uri);
+    $original_image = $this->container->get('image.factory')
+      ->get($original_uri);
     $this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize());
 
     if ($scheme == 'private') {
@@ -192,13 +231,15 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
 
       $this->drupalGet($generate_url_noaccess);
       $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
-      // Verify that images are not appended to the response. Currently this test only uses PNG images.
+      // Verify that images are not appended to the response.
+      // Currently this test only uses PNG images.
       if (strpos($generate_url, '.png') === FALSE) {
         $this->fail('Confirming that private image styles are not appended require PNG file.');
       }
       else {
-        // Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the
-        // response body.
+        // Check for PNG-Signature
+        // (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2)
+        // in the response body.
         $this->assertNoRaw(chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
       }
     }
@@ -215,7 +256,9 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
 
     // Allow insecure image derivatives to be created for the remainder of this
     // test.
-    $this->config('image.settings')->set('allow_insecure_derivatives', TRUE)->save();
+    $this->config('image.settings')
+      ->set('allow_insecure_derivatives', TRUE)
+      ->save();
 
     // Create another working copy of the file.
     $files = $this->drupalGetTestFiles('image');
@@ -239,7 +282,8 @@ class ImageStylesPathAndUrlTest extends WebTestBase {
     // Stop supressing the security token in the URL.
     $this->config('image.settings')->set('suppress_itok_output', FALSE)->save();
     // Ensure allow_insecure_derivatives is enabled.
-    $this->assertEqual($this->config('image.settings')->get('allow_insecure_derivatives'), TRUE);
+    $this->assertEqual($this->config('image.settings')
+      ->get('allow_insecure_derivatives'), TRUE);
     // Check that a security token is still required when generating a second
     // image derivative using the first one as a source.
     $nested_url = $this->style->buildUrl($generated_uri, $clean_url);