Commit b0c9fd8d authored by Steven Jones's avatar Steven Jones
Browse files

Add the start of some tests.

parent e11a166e
......@@ -5,3 +5,5 @@ services:
imageapi_optimize.hooks:
class: Drupal\imageapi_optimize\ImageAPIOptimizeHookImplementations
arguments: ['@string_translation']
imageapi_optimize.shell_operations:
class: Drupal\imageapi_optimize\ShellOperations
......@@ -6,6 +6,7 @@ use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Image\ImageInterface;
use Drupal\iamgeapi_optimize\ImageAPIOptimizeShellOperationsInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -21,13 +22,21 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
*/
protected $fileSystem;
/**
* The imageapi shell operation service.
*
* @var \Drupal\imageapi_optimize\ImageAPIOptimizeShellOperationsInterface
*/
protected $shellOperations;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, ImageFactory $image_factory, FileSystemInterface $file_system) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, ImageFactory $image_factory, FileSystemInterface $file_system, ImageAPIOptimizeShellOperationsInterface $shell_operatons) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $image_factory);
$this->fileSystem = $file_system;
$this->shellOperations = $shell_operatons;
}
/**
......@@ -40,7 +49,8 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
$plugin_definition,
$container->get('logger.factory')->get('imageapi_optimize'),
$container->get('image.factory'),
$container->get('file_system')
$container->get('file_system'),
$container->get('imageapi_optimize.shell_operations')
);
}
......@@ -106,13 +116,7 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
if (is_null($executable)) {
$executable = $this->executableName();
}
$output = array();
$return_var = 0;
$path = exec('which ' . escapeshellarg($executable), $output, $return_var);
if ($return_var == 0) {
return $path;
}
return FALSE;
return $this->shellOperations->findExecutablePath($executable);
}
/**
......@@ -129,18 +133,7 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
* Returns TRUE if the command completed successfully, FALSE otherwise.
*/
protected function execShellCommand($command, $options, $arguments) {
$output = array();
$return_val = 0;
$option_string = implode(' ', $options);
$argument_string = implode(' ', array_map('escapeshellarg', $arguments));
$last_line = exec(escapeshellcmd($command) . ' ' . $option_string . ' ' . $argument_string, $output, $return_val);
if ($return_val == 0) {
return TRUE;
}
else {
return FALSE;
}
return $this->shellOperations->execShellCommand($command, $options, $arguments);
}
/**
......@@ -157,20 +150,7 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
}
protected function saveCommandStdoutToFile($cmd, $dst) {
$return_val = 0;
ob_start();
passthru($cmd);
$output = ob_get_contents();
ob_end_clean();
file_unmanaged_save_data($output, $dst, FILE_EXISTS_REPLACE);
if ($return_val == 0) {
return TRUE;
}
else {
return FALSE;
}
return $this->shellOperations->saveCommandStdoutToFile($cmd, $dst);
}
public function getFullPathToBinary() {
......
<?php
namespace Drupal\iamgeapi_optimize;
interface ImageAPIOptimizeShellOperationsInterface {
public function findExecutablePath($executable = NULL);
public function execShellCommand($command, $options, $arguments);
public function saveCommandStdoutToFile($cmd, $dst);
}
<?php
namespace Drupal\imageapi_optimize;
/**
* Storage controller class for "image optimize pipeline" configuration entities.
*/
class ShellOperations implements ImageAPIOptimizeShellOperationsInterface {
/**
* Search the local system for the given executable binary.
*
* @param null $executable
* The name of the executable binary to find on the local system. If not
* specified the default executeable name for this class will be used.
*
* @return string|false
* The path to the binary on the local system, or FALSE if it could not be
* located.
*/
public function findExecutablePath($executable = NULL) {
$output = array();
$return_var = 0;
$path = exec('which ' . escapeshellarg($executable), $output, $return_var);
if ($return_var == 0) {
return $path;
}
return FALSE;
}
/**
* Execute a shell command on the local system.
*
* @param $command
* The command to execute.
* @param $options
* An array of options for the command. This will not be escaped before executing.
* @param $arguments
* An array of arguments for the command. These will be escaped.
*
* @return bool
* Returns TRUE if the command completed successfully, FALSE otherwise.
*/
public function execShellCommand($command, $options, $arguments) {
$output = array();
$return_val = 0;
$option_string = implode(' ', $options);
$argument_string = implode(' ', array_map('escapeshellarg', $arguments));
$last_line = exec(escapeshellcmd($command) . ' ' . $option_string . ' ' . $argument_string, $output, $return_val);
if ($return_val == 0) {
return TRUE;
}
else {
return FALSE;
}
}
public function saveCommandStdoutToFile($cmd, $dst) {
$return_val = 0;
ob_start();
passthru($cmd);
$output = ob_get_contents();
ob_end_clean();
file_unmanaged_save_data($output, $dst, FILE_EXISTS_REPLACE);
if ($return_val == 0) {
return TRUE;
}
else {
return FALSE;
}
}
}
<?php
namespace Drupal\Tests\imageapi_optimize\Unit;
use Drupal\imageapi_optimize\Plugin\ImageAPIOptimizeProcessor\AdvDef;
use Drupal\Tests\UnitTestCase;
/**
* Tests AdvDef image optimize plugin.
*
* @group imageapi_optimize
*/
class AdvDefTest extends UnitTestCase {
function testCase() {
$this->assertTrue(TRUE);
$advdefMock = $this->getMockBuilder('\Drupal\imageapi_optimize\Plugin\ImageAPIOptimizeProcessor\AdvDef')
->setMethods(['getFullPathToBinary', 'execShellCommand', 'getMimeType', 'sanitizeFilename'])
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->getMock();
$advdefMock->method('getFullPathToBinary')
->willReturn('/bin/advdef');
$advdefMock->method('sanitizeFilename')
->will($this->returnArgument(0));
$this->assertEquals('/bin/advdef', $advdefMock->getFullPathToBinary());
$this->assertEquals('some filename', $advdefMock->sanitizeFilename('some filename'));
$advdefMock->expects($this->once())
->method('execShellCommand')
->with($this->equalTo('something'));
$advdefMock->applyToImage('public://test_image.png');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment