You are here

class SanitizerHelperTest in SVG Upload Sanitizer 8

Unit test class for the SanitizeHelper class.

@package Drupal\Tests\svg_upload_sanitizer\Unit\Helper

@internal

Hierarchy

Expanded class hierarchy of SanitizerHelperTest

File

tests/src/Unit/Helper/SanitizerHelperTest.php, line 19

Namespace

Drupal\Tests\svg_upload_sanitizer\Unit\Helper
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SanitizerHelperTest::$fileSystem private property The mocked file system.
SanitizerHelperTest::$logger private property The logger.
SanitizerHelperTest::$sanitizerHelper private property The file helper to test.
SanitizerHelperTest::prepareFile private function
SanitizerHelperTest::setUp public function Overrides UnitTestCase::setUp
SanitizerHelperTest::testSanitize public function
SanitizerHelperTest::testSanitizeWhenFileDoesNotExist public function
SanitizerHelperTest::testSanitizeWhenMimeTypeIsNotSvg public function
SanitizerHelperTest::testSanitizeWhenRealpathIsNotResolved public function
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.