ContentValidatorTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Model\File;
  7. use Magento\Downloadable\Model\File\ContentValidator;
  8. class ContentValidatorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var ContentValidator
  12. */
  13. protected $validator;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $fileContentMock;
  18. protected function setUp()
  19. {
  20. $this->validator = new \Magento\Downloadable\Model\File\ContentValidator();
  21. $this->fileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  22. }
  23. public function testIsValid()
  24. {
  25. $this->fileContentMock->expects($this->any())->method('getFileData')
  26. ->will($this->returnValue(base64_encode('test content')));
  27. $this->fileContentMock->expects($this->any())->method('getName')
  28. ->will($this->returnValue('valid_name'));
  29. $this->assertTrue($this->validator->isValid($this->fileContentMock));
  30. }
  31. /**
  32. * @expectedException \Magento\Framework\Exception\InputException
  33. * @expectedExceptionMessage Provided content must be valid base64 encoded data.
  34. */
  35. public function testIsValidThrowsExceptionIfProvidedContentIsNotBase64Encoded()
  36. {
  37. $this->fileContentMock->expects($this->any())->method('getFileData')
  38. ->will($this->returnValue('not_a_base64_encoded_content'));
  39. $this->fileContentMock->expects($this->any())->method('getName')
  40. ->will($this->returnValue('valid_name'));
  41. $this->assertTrue($this->validator->isValid($this->fileContentMock));
  42. }
  43. /**
  44. * @expectedException \Magento\Framework\Exception\InputException
  45. * @expectedExceptionMessage Provided file name contains forbidden characters.
  46. * @dataProvider getInvalidNames
  47. * @param string $fileName
  48. */
  49. public function testIsValidThrowsExceptionIfProvidedImageNameContainsForbiddenCharacters($fileName)
  50. {
  51. $this->fileContentMock->expects($this->any())->method('getFileData')
  52. ->will($this->returnValue(base64_encode('test content')));
  53. $this->fileContentMock->expects($this->any())->method('getName')
  54. ->will($this->returnValue($fileName));
  55. $this->assertTrue($this->validator->isValid($this->fileContentMock));
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function getInvalidNames()
  61. {
  62. return [
  63. ['test\test'],
  64. ['test/test'],
  65. ['test:test'],
  66. ['test"test'],
  67. ['test*test'],
  68. ['test;test'],
  69. ['test?test'],
  70. ['test{test'],
  71. ['test}test'],
  72. ['test|test'],
  73. ['test(test'],
  74. ['test)test'],
  75. ['test<test'],
  76. ['test>test'],
  77. ];
  78. }
  79. }