summaryrefslogtreecommitdiffstats
path: root/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php
blob: 3e5aadd6f8e55c7c5462e1a43c2507efd48ab95d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Image\ToolkitTest.
 */

namespace Drupal\system\Tests\Image;

/**
 * Test that the functions in image.inc correctly pass data to the toolkit.
 */
class ToolkitTest extends ToolkitTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Image toolkit tests',
      'description' => 'Check image toolkit functions.',
      'group' => 'Image',
    );
  }

  /**
   * Check that hook_image_toolkits() is called and only available toolkits are
   * returned.
   */
  function testGetAvailableToolkits() {
    $toolkits = image_get_available_toolkits();
    $this->assertTrue(isset($toolkits['test']), 'The working toolkit was returned.');
    $this->assertFalse(isset($toolkits['broken']), 'The toolkit marked unavailable was not returned');
    $this->assertToolkitOperationsCalled(array());
  }

  /**
   * Test the image_load() function.
   */
  function testLoad() {
    $image = image_load($this->file, $this->toolkit);
    $this->assertTrue(is_object($image), 'Returned an object.');
    $this->assertEqual($this->toolkit, $image->toolkit, 'Image had toolkit set.');
    $this->assertToolkitOperationsCalled(array('load', 'get_info'));
  }

  /**
   * Test the image_save() function.
   */
  function testSave() {
    $this->assertFalse(image_save($this->image), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('save'));
  }

  /**
   * Test the image_resize() function.
   */
  function testResize() {
    $this->assertTrue(image_resize($this->image, 1, 2), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('resize'));

    // Check the parameters.
    $calls = image_test_get_all_calls();
    $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly');
    $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly');
  }

  /**
   * Test the image_scale() function.
   */
  function testScale() {
// TODO: need to test upscaling
    $this->assertTrue(image_scale($this->image, 10, 10), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('resize'));

    // Check the parameters.
    $calls = image_test_get_all_calls();
    $this->assertEqual($calls['resize'][0][1], 10, 'Width was passed correctly');
    $this->assertEqual($calls['resize'][0][2], 5, 'Height was based off aspect ratio and passed correctly');
  }

  /**
   * Test the image_scale_and_crop() function.
   */
  function testScaleAndCrop() {
    $this->assertTrue(image_scale_and_crop($this->image, 5, 10), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('resize', 'crop'));

    // Check the parameters.
    $calls = image_test_get_all_calls();

    $this->assertEqual($calls['crop'][0][1], 7.5, 'X was computed and passed correctly');
    $this->assertEqual($calls['crop'][0][2], 0, 'Y was computed and passed correctly');
    $this->assertEqual($calls['crop'][0][3], 5, 'Width was computed and passed correctly');
    $this->assertEqual($calls['crop'][0][4], 10, 'Height was computed and passed correctly');
  }

  /**
   * Test the image_rotate() function.
   */
  function testRotate() {
    $this->assertTrue(image_rotate($this->image, 90, 1), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('rotate'));

    // Check the parameters.
    $calls = image_test_get_all_calls();
    $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly');
    $this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly');
  }

  /**
   * Test the image_crop() function.
   */
  function testCrop() {
    $this->assertTrue(image_crop($this->image, 1, 2, 3, 4), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('crop'));

    // Check the parameters.
    $calls = image_test_get_all_calls();
    $this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly');
    $this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly');
    $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly');
    $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly');
  }

  /**
   * Test the image_desaturate() function.
   */
  function testDesaturate() {
    $this->assertTrue(image_desaturate($this->image), 'Function returned the expected value.');
    $this->assertToolkitOperationsCalled(array('desaturate'));

    // Check the parameters.
    $calls = image_test_get_all_calls();
    $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.');
  }
}