123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cms\Test\Unit\Helper\Wysiwyg;
- use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ImagesTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Cms\Helper\Wysiwyg\Images
- */
- protected $imagesHelper;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $filesystemMock;
- /**
- * @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $directoryWriteMock;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeMock;
- /**
- * @var \Magento\Framework\App\Helper\Context|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $contextMock;
- /**
- * @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $eventManagerMock;
- /**
- * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $requestMock;
- /**
- * @var \Magento\Framework\Url\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $urlEncoderMock;
- /**
- * @var \Magento\Backend\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $backendDataMock;
- /**
- * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $escaperMock;
- /**
- * @var string
- */
- protected $path;
- protected function setUp()
- {
- $this->path = 'PATH';
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
- $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
- $this->urlEncoderMock = $this->createMock(\Magento\Framework\Url\EncoderInterface::class);
- $this->backendDataMock = $this->createMock(\Magento\Backend\Helper\Data::class);
- $this->contextMock = $this->createMock(\Magento\Framework\App\Helper\Context::class);
- $this->contextMock->expects($this->any())
- ->method('getEventManager')
- ->willReturn($this->eventManagerMock);
- $this->contextMock->expects($this->any())
- ->method('getRequest')
- ->willReturn($this->requestMock);
- $this->contextMock->expects($this->any())
- ->method('getUrlEncoder')
- ->willReturn($this->urlEncoderMock);
- $this->directoryWriteMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Write::class)
- ->setConstructorArgs(['path' => $this->path])
- ->disableOriginalConstructor()
- ->getMock();
- $this->directoryWriteMock->expects($this->any())
- ->method('getAbsolutePath')
- ->willReturnMap(
- [
- [WysiwygConfig::IMAGE_DIRECTORY, null, $this->getAbsolutePath(WysiwygConfig::IMAGE_DIRECTORY)],
- [null, null, $this->getAbsolutePath(null)],
- ['', null, $this->getAbsolutePath('')],
- ]
- );
- $this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
- $this->filesystemMock->expects($this->once())
- ->method('getDirectoryWrite')
- ->willReturn($this->directoryWriteMock);
- $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
- ->setMethods(
- [
- 'clearWebsiteCache', 'getDefaultStoreView', 'getGroup', 'getGroups',
- 'getStore', 'getStores', 'getWebsite', 'getWebsites', 'hasSingleStore',
- 'isSingleStoreMode', 'reinitStores', 'setCurrentStore', 'setIsSingleStoreModeAllowed',
- ]
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $this->escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
- $this->imagesHelper = $this->objectManager->getObject(
- \Magento\Cms\Helper\Wysiwyg\Images::class,
- [
- 'context' => $this->contextMock,
- 'filesystem' => $this->filesystemMock,
- 'storeManager' => $this->storeManagerMock,
- 'backendData' => $this->backendDataMock,
- 'escaper' => $this->escaperMock,
- ]
- );
- }
- protected function tearDown()
- {
- $this->objectManager = null;
- $this->directoryWriteMock = null;
- $this->filesystemMock = null;
- $this->storeManagerMock = null;
- $this->storeMock = null;
- $this->imagesHelper = null;
- $this->contextMock = null;
- $this->eventManagerMock = null;
- $this->requestMock = null;
- $this->urlEncoderMock = null;
- $this->backendDataMock = null;
- $this->escaperMock = null;
- }
- /**
- * @param string $path
- * @return string
- */
- protected function getAbsolutePath($path)
- {
- return $this->path . $path;
- }
- public function testSetStoreId()
- {
- $this->assertEquals($this->imagesHelper, $this->imagesHelper->setStoreId(1));
- }
- public function testGetStorageRoot()
- {
- $this->assertEquals(
- $this->getAbsolutePath(''),
- $this->imagesHelper->getStorageRoot()
- );
- }
- public function testGetBaseUrl()
- {
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->storeMock->expects($this->once())
- ->method('getBaseUrl')
- ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
- $this->imagesHelper->getBaseUrl();
- }
- public function testGetTreeNodeName()
- {
- $this->assertEquals('node', $this->imagesHelper->getTreeNodeName());
- }
- public function testConvertPathToId()
- {
- $pathOne = '/test_path';
- $pathTwo = $this->getAbsolutePath('') . '/test_path';
- $this->assertEquals(
- $this->imagesHelper->convertPathToId($pathOne),
- $this->imagesHelper->convertPathToId($pathTwo)
- );
- }
- /**
- * @param string $path
- * @param string $pathId
- * @dataProvider providerConvertIdToPath
- */
- public function testConvertIdToPathAnyId($path, $pathId)
- {
- $pathOne = $this->imagesHelper->getStorageRoot() . $path;
- $pathTwo = $this->imagesHelper->convertIdToPath($pathId);
- $this->assertEquals($pathOne, $pathTwo);
- }
- /**
- * @return array
- */
- public function providerConvertIdToPath()
- {
- return [
- ['', ''],
- ['/test_path', 'L3Rlc3RfcGF0aA--'],
- ];
- }
- public function testConvertIdToPathNodeRoot()
- {
- $pathId = \Magento\Theme\Helper\Storage::NODE_ROOT;
- $this->assertEquals($this->imagesHelper->getStorageRoot(), $this->imagesHelper->convertIdToPath($pathId));
- }
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Path is invalid
- */
- public function testConvertIdToPathInvalid()
- {
- $this->imagesHelper->convertIdToPath('Ly4uLy4uLy4uLy4uLy4uL3dvcms-');
- }
- /**
- * @param string $fileName
- * @param int $maxLength
- * @param string $expectedFilename
- * @dataProvider providerShortFilename
- */
- public function testGetShortFilename($fileName, $maxLength, $expectedFilename)
- {
- $this->assertEquals($expectedFilename, $this->imagesHelper->getShortFilename($fileName, $maxLength));
- }
- /**
- * @return array
- */
- public function providerShortFilename()
- {
- return [
- ['test', 3, 'tes...'],
- ['test', 4, 'test'],
- ['test', 20, 'test'],
- ];
- }
- /**
- * @param string $fileName
- * @param string $expectedFilename
- * @dataProvider providerShortFilenameDefaultMaxLength
- */
- public function testGetShortFilenameDefaultMaxLength($fileName, $expectedFilename)
- {
- $this->assertEquals($expectedFilename, $this->imagesHelper->getShortFilename($fileName));
- }
- /**
- * @return array
- */
- public function providerShortFilenameDefaultMaxLength()
- {
- return [
- ['Mini text', 'Mini text'],
- ['20 symbols are here', '20 symbols are here'],
- ['Some text for this unit test', 'Some text for this u...'],
- ];
- }
- /**
- * @param bool $allowedValue
- * @dataProvider providerIsUsingStaticUrlsAllowed
- */
- public function testIsUsingStaticUrlsAllowed($allowedValue)
- {
- $this->generalSettingsIsUsingStaticUrlsAllowed($allowedValue);
- $this->assertEquals($allowedValue, $this->imagesHelper->isUsingStaticUrlsAllowed());
- }
- /**
- * @param bool $allowedValue
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- protected function generalSettingsIsUsingStaticUrlsAllowed($allowedValue)
- {
- $storeId = 1;
- $this->imagesHelper->setStoreId($storeId);
- $checkResult = new \stdClass();
- $checkResult->isAllowed = false;
- $this->eventManagerMock->expects($this->any())
- ->method('dispatch')
- ->with('cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $storeId])
- ->willReturnCallback(function ($str, $arr) use ($allowedValue) {
- $arr['result']->isAllowed = $allowedValue;
- });
- }
- /**
- * @return array
- */
- public function providerIsUsingStaticUrlsAllowed()
- {
- return [
- [true],
- [false],
- ];
- }
- /**
- * @param string $pathId
- * @param string $expectedPath
- * @param bool $isExist
- * @dataProvider providerGetCurrentPath
- */
- public function testGetCurrentPath($pathId, $expectedPath, $isExist)
- {
- $this->requestMock->expects($this->any())
- ->method('getParam')
- ->willReturnMap(
- [
- ['node', null, $pathId],
- ]
- );
- $this->directoryWriteMock->expects($this->any())
- ->method('isDirectory')
- ->willReturnMap(
- [
- ['/../test_path', true],
- ['/../my.jpg', false],
- ['.', true],
- ]
- );
- $this->directoryWriteMock->expects($this->any())
- ->method('getRelativePath')
- ->willReturnMap(
- [
- ['PATH/test_path', '/../test_path'],
- ['PATH/my.jpg', '/../my.jpg'],
- ['PATH', '.'],
- ]
- );
- $this->directoryWriteMock->expects($this->once())
- ->method('isExist')
- ->willReturn($isExist);
- $this->directoryWriteMock->expects($this->any())
- ->method('create')
- ->with($this->directoryWriteMock->getRelativePath($expectedPath));
- $this->assertEquals($expectedPath, $this->imagesHelper->getCurrentPath());
- }
- public function testGetCurrentPathThrowException()
- {
- $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
- $this->expectExceptionMessage('The directory PATH is not writable by server.');
- $this->directoryWriteMock->expects($this->once())
- ->method('isExist')
- ->willReturn(false);
- $this->directoryWriteMock->expects($this->any())
- ->method('create')
- ->willThrowException(
- new \Magento\Framework\Exception\FileSystemException(__('Could not create a directory.'))
- );
- $this->imagesHelper->getCurrentPath();
- $this->fail('An expected exception has not been raised.');
- }
- /**
- * @return array
- */
- public function providerGetCurrentPath()
- {
- return [
- ['L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
- ['L215LmpwZw--', 'PATH', true],
- [null, 'PATH', true],
- ['L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
- ['L215LmpwZw--', 'PATH', false],
- [null, 'PATH', false],
- ];
- }
- public function testGetCurrentUrl()
- {
- $storeId = 1;
- $baseUrl = 'http://localhost';
- $relativePath = '/../wysiwyg';
- $this->imagesHelper->setStoreId($storeId);
- $this->storeMock->expects($this->once())
- ->method('getBaseUrl')
- ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
- ->willReturn($baseUrl);
- $this->storeManagerMock->expects($this->once())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->directoryWriteMock->expects($this->any())
- ->method('getRelativePath')
- ->willReturn($relativePath);
- $this->assertEquals($baseUrl . $relativePath . '/', $this->imagesHelper->getCurrentUrl());
- }
- /**
- * @param string $baseUrl
- * @param string $fileName
- * @param bool $isUsingStaticUrls
- * @param string|null $escapedValue
- * @param string $expectedHtml
- * @dataProvider providerGetImageHtmlDeclarationRenderingAsTag
- */
- public function testGetImageHtmlDeclarationRenderingAsTag(
- $baseUrl,
- $fileName,
- $isUsingStaticUrls,
- $escapedValue,
- $expectedHtml
- ) {
- $this->generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls, $escapedValue);
- $this->assertEquals($expectedHtml, $this->imagesHelper->getImageHtmlDeclaration($fileName, true));
- }
- /**
- * @return array
- */
- public function providerGetImageHtmlDeclarationRenderingAsTag()
- {
- return [
- [
- 'http://localhost',
- 'test.png',
- true,
- null,
- '<img src="http://localhost/test.png" alt="" />',
- ],
- [
- 'http://localhost',
- 'test.png',
- false,
- '{{media url="/test.png"}}',
- '<img src="{{media url="/test.png"}}" alt="" />',
- ],
- ];
- }
- /**
- * @param string $baseUrl
- * @param string $fileName
- * @param bool $isUsingStaticUrls
- * @param string $expectedHtml
- * @dataProvider providerGetImageHtmlDeclaration
- */
- public function testGetImageHtmlDeclaration($baseUrl, $fileName, $isUsingStaticUrls, $expectedHtml)
- {
- $directive = '{{media url="/' . $fileName . '"}}';
- $this->generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls);
- $this->urlEncoderMock->expects($this->any())
- ->method('encode')
- ->with($directive)
- ->willReturn($directive);
- $this->backendDataMock->expects($this->any())
- ->method('getUrl')
- ->with('cms/wysiwyg/directive', ['___directive' => $directive, '_escape_params' => false])
- ->willReturn($directive);
- $this->assertEquals($expectedHtml, $this->imagesHelper->getImageHtmlDeclaration($fileName));
- }
- /**
- * @return array
- */
- public function providerGetImageHtmlDeclaration()
- {
- return [
- ['http://localhost', 'test.png', true, 'http://localhost/test.png'],
- ['http://localhost', 'test.png', false, '{{media url="/test.png"}}'],
- ];
- }
- /**
- * @param string $baseUrl
- * @param bool $isUsingStaticUrls
- * @param string|null $escapedValue
- */
- protected function generalSettingsGetImageHtmlDeclaration($baseUrl, $isUsingStaticUrls, $escapedValue = null)
- {
- $storeId = 1;
- $this->imagesHelper->setStoreId($storeId);
- $this->storeMock->expects($this->any())
- ->method('getBaseUrl')
- ->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
- ->willReturn($baseUrl);
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->willReturn($this->storeMock);
- if ($escapedValue) {
- $this->escaperMock->expects($this->once())->method('escapeHtml')->willReturn($escapedValue);
- }
- $this->generalSettingsIsUsingStaticUrlsAllowed($isUsingStaticUrls);
- }
- }
|