You are here

public function ToolkitGdTest::testResourceDestruction in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Image/ToolkitGdTest.php \Drupal\system\Tests\Image\ToolkitGdTest::testResourceDestruction()

Tests that GD resources are freed from memory.

File

core/modules/system/src/Tests/Image/ToolkitGdTest.php, line 417
Contains \Drupal\system\Tests\Image\ToolkitGdTest.

Class

ToolkitGdTest
Tests that core image manipulations work properly: scale, resize, rotate, crop, scale and crop, and desaturate.

Namespace

Drupal\system\Tests\Image

Code

public function testResourceDestruction() {

  // Test that an Image object going out of scope releases its GD resource.
  $image = $this->imageFactory
    ->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png');
  $res = $image
    ->getToolkit()
    ->getResource();
  $this
    ->assertTrue(is_resource($res), 'Successfully loaded image resource.');
  $image = NULL;
  $this
    ->assertFalse(is_resource($res), 'Image resource was destroyed after losing scope.');

  // Test that 'create_new' operation does not leave orphaned GD resources.
  $image = $this->imageFactory
    ->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png');
  $old_res = $image
    ->getToolkit()
    ->getResource();

  // Check if resource has been created successfully.
  $this
    ->assertTrue(is_resource($old_res));
  $image
    ->createNew(20, 20);
  $new_res = $image
    ->getToolkit()
    ->getResource();

  // Check if the original resource has been destroyed.
  $this
    ->assertFalse(is_resource($old_res));

  // Check if a new resource has been created successfully.
  $this
    ->assertTrue(is_resource($new_res));
}