ToolkitTest.php 3.5 KB

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