Skip to content
Snippets Groups Projects
Commit 2a947d16 authored by Steven Jones's avatar Steven Jones
Browse files

Revert "Add the start of some tests."

This reverts commit b0c9fd8d.
parent e7e47f88
Branches
Tags
No related merge requests found
......@@ -5,5 +5,3 @@ services:
imageapi_optimize.hooks:
class: Drupal\imageapi_optimize\ImageAPIOptimizeHookImplementations
arguments: ['@string_translation']
imageapi_optimize.shell_operations:
class: Drupal\imageapi_optimize\ShellOperations
......@@ -6,7 +6,6 @@ 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;
......@@ -22,21 +21,13 @@ 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, ImageAPIOptimizeShellOperationsInterface $shell_operatons) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, ImageFactory $image_factory, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $image_factory);
$this->fileSystem = $file_system;
$this->shellOperations = $shell_operatons;
}
/**
......@@ -49,8 +40,7 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
$plugin_definition,
$container->get('logger.factory')->get('imageapi_optimize'),
$container->get('image.factory'),
$container->get('file_system'),
$container->get('imageapi_optimize.shell_operations')
$container->get('file_system')
);
}
......@@ -116,7 +106,13 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
if (is_null($executable)) {
$executable = $this->executableName();
}
return $this->shellOperations->findExecutablePath($executable);
$output = array();
$return_var = 0;
$path = exec('which ' . escapeshellarg($executable), $output, $return_var);
if ($return_var == 0) {
return $path;
}
return FALSE;
}
/**
......@@ -133,7 +129,18 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
* Returns TRUE if the command completed successfully, FALSE otherwise.
*/
protected function execShellCommand($command, $options, $arguments) {
return $this->shellOperations->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;
}
}
/**
......@@ -150,7 +157,20 @@ abstract class ImageAPIOptimizeProcessorBinaryBase extends ConfigurableImageAPIO
}
protected function saveCommandStdoutToFile($cmd, $dst) {
return $this->shellOperations->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;
}
}
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');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment