class SanitizerHelperTest in SVG Upload Sanitizer 8
Unit test class for the SanitizeHelper class.
@package Drupal\Tests\svg_upload_sanitizer\Unit\Helper
@internal
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\svg_upload_sanitizer\Unit\Helper\SanitizerHelperTest
Expanded class hierarchy of SanitizerHelperTest
File
- tests/
src/ Unit/ Helper/ SanitizerHelperTest.php, line 19
Namespace
Drupal\Tests\svg_upload_sanitizer\Unit\HelperView source
class SanitizerHelperTest extends UnitTestCase {
/**
* The mocked file system.
*
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $fileSystem;
/**
* The logger.
*
* @var \Drupal\Tests\svg_upload_sanitizer\TestLogger
*/
private $logger;
/**
* The file helper to test.
*
* @var \Drupal\svg_upload_sanitizer\Helper\SanitizerHelper
*/
private $sanitizerHelper;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->fileSystem = $this
->createMock(FileSystemInterface::class);
$this->logger = new TestLogger();
$this->sanitizerHelper = new SanitizerHelper($this->fileSystem, new Sanitizer());
$this->sanitizerHelper
->setLogger($this->logger);
}
public function testSanitizeWhenMimeTypeIsNotSvg() {
$file = $this
->createMock(FileInterface::class);
$file
->expects($this
->atLeastOnce())
->method('getMimeType')
->willReturn('image/png');
$this
->assertFalse($this->sanitizerHelper
->sanitize($file));
}
public function testSanitizeWhenRealpathIsNotResolved() {
list($file) = $this
->prepareFile(FALSE);
$this
->assertFalse($this->sanitizerHelper
->sanitize($file));
$logs = $this->logger
->getLogs('notice');
$this
->assertCount(1, $logs);
$this
->assertSame('Could not resolve the path of the file (URI: "public://fileuri").', reset($logs));
}
public function testSanitizeWhenFileDoesNotExist() {
list($file) = $this
->prepareFile(TRUE);
$this
->assertFalse($this->sanitizerHelper
->sanitize($file));
$logs = $this->logger
->getLogs('notice');
$this
->assertCount(1, $logs);
$this
->assertSame('The file does not exist (path: "something/that/will/never/exists.casper").', reset($logs));
}
public function testSanitize() {
list($file) = $this
->prepareFile(TRUE, TRUE);
$this
->assertTrue($this->sanitizerHelper
->sanitize($file));
}
private function prepareFile($pathIsResolved, $filePathExists = FALSE) {
$filePath = $filePathExists ? sprintf('%s/fixtures/image.svg', __DIR__) : 'something/that/will/never/exists.casper';
$fileUri = 'public://fileuri';
$file = $this
->createMock(FileInterface::class);
$file
->expects($this
->atLeastOnce())
->method('getMimeType')
->willReturn('image/svg+xml');
$file
->expects($this
->atLeastOnce())
->method('getFileUri')
->willReturn($fileUri);
$this->fileSystem
->expects($this
->atLeastOnce())
->method('realpath')
->with($fileUri)
->willReturn($pathIsResolved ? $filePath : FALSE);
return [
$file,
$filePath,
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SanitizerHelperTest:: |
private | property | The mocked file system. | |
SanitizerHelperTest:: |
private | property | The logger. | |
SanitizerHelperTest:: |
private | property | The file helper to test. | |
SanitizerHelperTest:: |
private | function | ||
SanitizerHelperTest:: |
public | function |
Overrides UnitTestCase:: |
|
SanitizerHelperTest:: |
public | function | ||
SanitizerHelperTest:: |
public | function | ||
SanitizerHelperTest:: |
public | function | ||
SanitizerHelperTest:: |
public | function | ||
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |