ToolkitTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Drupal\FunctionalTests\Image;
  3. /**
  4. * Tests image toolkit functions.
  5. *
  6. * @group Image
  7. */
  8. class ToolkitTest extends ToolkitTestBase {
  9. /**
  10. * Check that ImageToolkitManager::getAvailableToolkits() only returns
  11. * available toolkits.
  12. */
  13. public function testGetAvailableToolkits() {
  14. $manager = $this->container->get('image.toolkit.manager');
  15. $toolkits = $manager->getAvailableToolkits();
  16. $this->assertTrue(isset($toolkits['test']), 'The working toolkit was returned.');
  17. $this->assertTrue(isset($toolkits['test:derived_toolkit']), 'The derived toolkit was returned.');
  18. $this->assertFalse(isset($toolkits['broken']), 'The toolkit marked unavailable was not returned');
  19. $this->assertToolkitOperationsCalled([]);
  20. }
  21. /**
  22. * Tests Image's methods.
  23. */
  24. public function testLoad() {
  25. $image = $this->getImage();
  26. $this->assertTrue(is_object($image), 'Returned an object.');
  27. $this->assertEqual($image->getToolkitId(), 'test', 'Image had toolkit set.');
  28. $this->assertToolkitOperationsCalled(['parseFile']);
  29. }
  30. /**
  31. * Test the image_save() function.
  32. */
  33. public function testSave() {
  34. $this->assertFalse($this->image->save(), 'Function returned the expected value.');
  35. $this->assertToolkitOperationsCalled(['save']);
  36. }
  37. /**
  38. * Test the image_apply() function.
  39. */
  40. public function testApply() {
  41. $data = ['p1' => 1, 'p2' => TRUE, 'p3' => 'text'];
  42. $this->assertTrue($this->image->apply('my_operation', $data), 'Function returned the expected value.');
  43. // Check that apply was called and with the correct parameters.
  44. $this->assertToolkitOperationsCalled(['apply']);
  45. $calls = $this->imageTestGetAllCalls();
  46. $this->assertEqual($calls['apply'][0][0], 'my_operation', "'my_operation' was passed correctly as operation");
  47. $this->assertEqual($calls['apply'][0][1]['p1'], 1, 'integer parameter p1 was passed correctly');
  48. $this->assertEqual($calls['apply'][0][1]['p2'], TRUE, 'boolean parameter p2 was passed correctly');
  49. $this->assertEqual($calls['apply'][0][1]['p3'], 'text', 'string parameter p3 was passed correctly');
  50. }
  51. /**
  52. * Test the image_apply() function.
  53. */
  54. public function testApplyNoParameters() {
  55. $this->assertTrue($this->image->apply('my_operation'), 'Function returned the expected value.');
  56. // Check that apply was called and with the correct parameters.
  57. $this->assertToolkitOperationsCalled(['apply']);
  58. $calls = $this->imageTestGetAllCalls();
  59. $this->assertEqual($calls['apply'][0][0], 'my_operation', "'my_operation' was passed correctly as operation");
  60. $this->assertEqual($calls['apply'][0][1], [], 'passing no parameters was handled correctly');
  61. }
  62. /**
  63. * Tests image toolkit operations inheritance by derivative toolkits.
  64. */
  65. public function testDerivative() {
  66. $toolkit_manager = $this->container->get('image.toolkit.manager');
  67. $operation_manager = $this->container->get('image.toolkit.operation.manager');
  68. $toolkit = $toolkit_manager->createInstance('test:derived_toolkit');
  69. // Load an overwritten and an inherited operation.
  70. $blur = $operation_manager->getToolkitOperation($toolkit, 'blur');
  71. $invert = $operation_manager->getToolkitOperation($toolkit, 'invert');
  72. $this->assertIdentical('foo_derived', $blur->getPluginId(), "'Blur' operation overwritten by derivative.");
  73. $this->assertIdentical('bar', $invert->getPluginId(), '"Invert" operation inherited from base plugin.');
  74. }
  75. }