1 image.test ImageStyleFloodProtection::testFloodProtection()

Check the number of images that can be generated simultaneously.

File

core/modules/image/tests/image.test, line 1797
Tests for image.module.

Class

ImageStyleFloodProtection
Tests the functions for generating paths and URLs for image styles.

Code

function testFloodProtection() {
  // Copy sample images.
  $files = $this->backdropGetTestFiles('image');
  $file = reset($files);
  $image1 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  $image2 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  $image3 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  $image4 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  $image5 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
  $image6 = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);

  // Check that the default value of 5 max images is respected.
  for ($n = 0; $n < 6; $n++) {
    $flood_lock_name = image_style_flood_lock_name();
    if ($n < 5) {
      $this->assertTrue(!empty($flood_lock_name), 'Lock ' . ($n + 1) . ' acquired for generating images.');
    }
    else {
      $this->assertFalse($flood_lock_name, 'Lock ' . ($n + 1) . ' correctly not acquired');
    }
  }

  // Release all locks created in the calls to image_style_flood_lock_name().
  lock_release_all();

  // Reduce the threshold down to 2 unauthorized images at a time.
  config_set('system.core', 'image_style_flood_limit', 2);

  // The image_module_test_wait_effect effect has a 5 second delay on it. Make
  // 3 simultaneous requests and check that the 3rd one returns a 403.
  $images = array(
    image_style_url($this->style_name, $image1),
    image_style_url($this->style_name, $image2),
    image_style_url($this->style_name, $image3),
  );

  // Remove the whitelist to test unauthorized requests.
  $added_uris = &image_style_add_allowed_uri();
  $added_uris = array();

  // Without the whitelist, only 2 unauthorized requests should be allowed.
  $this->assertMultiStyleGenerated($images, 2);

  // Make the list again, this time saving the whitelist immediately.
  $images = array(
    image_style_url($this->style_name, $image1),
    image_style_url($this->style_name, $image2),
    image_style_url($this->style_name, $image3),
    image_style_url($this->style_name, $image4),
    image_style_url($this->style_name, $image5),
    image_style_url($this->style_name, $image6),
  );
  // Save the whitelist.
  image_style_save_allowed_uris();

  // Now all of these images should be allowed.
  $this->assertMultiStyleGenerated($images, 6);
}