Skip to content

Commit 55cf2dc

Browse files
Rajneesh Guptarajneesh1dev
authored andcommitted
String validation for filename added and updated unit test case #13937.
1 parent cf69967 commit 55cf2dc

File tree

3 files changed

+211
-55
lines changed

3 files changed

+211
-55
lines changed

app/code/Magento/Sitemap/Block/Adminhtml/Edit/Form.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ protected function _prepareForm()
7373
'name' => 'sitemap_filename',
7474
'required' => true,
7575
'note' => __('example: sitemap.xml'),
76-
'value' => $model->getSitemapFilename()
76+
'value' => $model->getSitemapFilename(),
77+
'class' => 'validate-length maximum-length-32'
7778
]
7879
);
7980

app/code/Magento/Sitemap/Controller/Adminhtml/Sitemap/Save.php

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,61 @@
1111

1212
class Save extends \Magento\Sitemap\Controller\Adminhtml\Sitemap
1313
{
14+
/**
15+
* Maximum length of sitemap filename
16+
*/
17+
const MAX_FILENAME_LENGTH = 32;
18+
19+
/**
20+
* @var $_stringValidator
21+
*/
22+
public $_stringValidator;
23+
24+
/**
25+
* @var $_pathValidator
26+
*/
27+
public $_pathValidator;
28+
29+
/**
30+
* @var $_sitemapHelper
31+
*/
32+
public $_sitemapHelper;
33+
34+
/**
35+
* @var $_filesystem
36+
*/
37+
public $_filesystem;
38+
39+
/**
40+
* @var $_sitemapFactory
41+
*/
42+
public $_sitemapFactory;
43+
44+
/**
45+
* Save constructor.
46+
* @param Action\Context $context
47+
* @param \Magento\Framework\Validator\StringLength $stringValidator
48+
* @param \Magento\MediaStorage\Model\File\Validator\AvailablePath $pathValidator
49+
* @param \Magento\Sitemap\Helper\Data $sitemapHelper
50+
* @param \Magento\Framework\Filesystem $filesystem
51+
* @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory
52+
*/
53+
public function __construct(
54+
\Magento\Backend\App\Action\Context $context,
55+
\Magento\Framework\Validator\StringLength $stringValidator,
56+
\Magento\MediaStorage\Model\File\Validator\AvailablePath $pathValidator,
57+
\Magento\Sitemap\Helper\Data $sitemapHelper,
58+
\Magento\Framework\Filesystem $filesystem,
59+
\Magento\Sitemap\Model\SitemapFactory $sitemapFactory
60+
) {
61+
parent::__construct($context);
62+
$this->_stringValidator = $stringValidator;
63+
$this->_pathValidator = $pathValidator;
64+
$this->_sitemapHelper = $sitemapHelper;
65+
$this->_filesystem = $filesystem;
66+
$this->_sitemapFactory = $sitemapFactory;
67+
}
68+
1469
/**
1570
* Validate path for generation
1671
*
@@ -23,17 +78,25 @@ protected function validatePath(array $data)
2378
if (!empty($data['sitemap_filename']) && !empty($data['sitemap_path'])) {
2479
$data['sitemap_path'] = '/' . ltrim($data['sitemap_path'], '/');
2580
$path = rtrim($data['sitemap_path'], '\\/') . '/' . $data['sitemap_filename'];
26-
/** @var $validator \Magento\MediaStorage\Model\File\Validator\AvailablePath */
27-
$validator = $this->_objectManager->create(\Magento\MediaStorage\Model\File\Validator\AvailablePath::class);
28-
/** @var $helper \Magento\Sitemap\Helper\Data */
29-
$helper = $this->_objectManager->get(\Magento\Sitemap\Helper\Data::class);
30-
$validator->setPaths($helper->getValidPaths());
31-
if (!$validator->isValid($path)) {
32-
foreach ($validator->getMessages() as $message) {
81+
$this->_pathValidator->setPaths($this->_sitemapHelper->getValidPaths());
82+
if (!$this->_pathValidator->isValid($path)) {
83+
foreach ($this->_pathValidator->getMessages() as $message) {
84+
$this->messageManager->addErrorMessage($message);
85+
}
86+
// save data in session
87+
$this->_session->setFormData($data);
88+
// redirect to edit form
89+
return false;
90+
}
91+
92+
$filename = rtrim($data['sitemap_filename']);
93+
$this->_stringValidator->setMax(self::MAX_FILENAME_LENGTH);
94+
if (!$this->_stringValidator->isValid($filename)) {
95+
foreach ($this->_stringValidator->getMessages() as $message) {
3396
$this->messageManager->addErrorMessage($message);
3497
}
3598
// save data in session
36-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
99+
$this->_session->setFormData($data);
37100
// redirect to edit form
38101
return false;
39102
}
@@ -49,9 +112,8 @@ protected function validatePath(array $data)
49112
*/
50113
protected function clearSiteMap(\Magento\Sitemap\Model\Sitemap $model)
51114
{
52-
/** @var \Magento\Framework\Filesystem\Directory\Write $directory */
53-
$directory = $this->_objectManager->get(\Magento\Framework\Filesystem::class)
54-
->getDirectoryWrite(DirectoryList::ROOT);
115+
/** @var \Magento\Framework\Filesystem $directory */
116+
$directory = $this->_filesystem->getDirectoryWrite(DirectoryList::ROOT);
55117

56118
if ($this->getRequest()->getParam('sitemap_id')) {
57119
$model->load($this->getRequest()->getParam('sitemap_id'));
@@ -74,7 +136,7 @@ protected function saveData($data)
74136
{
75137
// init model and set data
76138
/** @var \Magento\Sitemap\Model\Sitemap $model */
77-
$model = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
139+
$model = $this->_sitemapFactory->create();
78140
$this->clearSiteMap($model);
79141
$model->setData($data);
80142

@@ -85,13 +147,13 @@ protected function saveData($data)
85147
// display success message
86148
$this->messageManager->addSuccessMessage(__('You saved the sitemap.'));
87149
// clear previously saved data from session
88-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData(false);
150+
$this->_session->setFormData(false);
89151
return $model->getId();
90152
} catch (\Exception $e) {
91153
// display error message
92154
$this->messageManager->addErrorMessage($e->getMessage());
93155
// save data in session
94-
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setFormData($data);
156+
$this->_session->setFormData($data);
95157
}
96158
return false;
97159
}

app/code/Magento/Sitemap/Test/Unit/Controller/Adminhtml/Sitemap/SaveTest.php

Lines changed: 133 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
99
use Magento\Framework\Controller\ResultFactory;
10+
use Magento\Sitemap\Controller\Adminhtml\Sitemap\Save;
1011

1112
class SaveTest extends \PHPUnit\Framework\TestCase
1213
{
@@ -18,12 +19,7 @@ class SaveTest extends \PHPUnit\Framework\TestCase
1819
/**
1920
* @var \Magento\Backend\App\Action\Context
2021
*/
21-
protected $context;
22-
23-
/**
24-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
25-
*/
26-
protected $objectManagerHelper;
22+
protected $contextMock;
2723

2824
/**
2925
* @var \Magento\Framework\HTTP\PhpEnvironment\Request|\PHPUnit_Framework_MockObject_MockObject
@@ -50,8 +46,41 @@ class SaveTest extends \PHPUnit\Framework\TestCase
5046
*/
5147
protected $messageManagerMock;
5248

49+
/**
50+
* @var \Magento\Framework\Validator\StringLength|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
protected $lengthValidator;
53+
54+
/**
55+
* @var \Magento\MediaStorage\Model\File\Validator\AvailablePath|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
protected $pathValidator;
58+
59+
/**
60+
* @var \Magento\Sitemap\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
61+
*/
62+
protected $helper;
63+
64+
/**
65+
* @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
66+
*/
67+
protected $fileSystem;
68+
69+
/**
70+
* @var \Magento\Sitemap\Model\SitemapFactory|\PHPUnit_Framework_MockObject_MockObject
71+
*/
72+
protected $siteMapFactory;
73+
74+
/**
75+
* @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject
76+
*/
77+
protected $session;
78+
5379
protected function setUp()
5480
{
81+
$this->contextMock = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
82+
->disableOriginalConstructor()
83+
->getMock();
5584
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
5685
->disableOriginalConstructor()
5786
->setMethods(['getPostValue'])
@@ -66,27 +95,48 @@ protected function setUp()
6695
->getMock();
6796
$this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
6897
->getMock();
69-
98+
$this->helper = $this->getMockBuilder(\Magento\Sitemap\Helper\Data::class)
99+
->disableOriginalConstructor()
100+
->getMock();
70101
$this->resultFactoryMock->expects($this->once())
71102
->method('create')
72103
->with(ResultFactory::TYPE_REDIRECT)
73104
->willReturn($this->resultRedirectMock);
105+
$this->session = $this->getMockBuilder(\Magento\Backend\Model\Session::class)
106+
->disableOriginalConstructor()
107+
->setMethods(['setFormData'])
108+
->getMock();
74109

75-
$this->objectManagerHelper = new ObjectManagerHelper($this);
76-
$this->context = $this->objectManagerHelper->getObject(
77-
\Magento\Backend\App\Action\Context::class,
78-
[
79-
'resultFactory' => $this->resultFactoryMock,
80-
'request' => $this->requestMock,
81-
'messageManager' => $this->messageManagerMock,
82-
'objectManager' => $this->objectManagerMock
83-
]
84-
);
85-
$this->saveController = $this->objectManagerHelper->getObject(
86-
\Magento\Sitemap\Controller\Adminhtml\Sitemap\Save::class,
87-
[
88-
'context' => $this->context
89-
]
110+
$this->contextMock->expects($this->once())
111+
->method('getMessageManager')
112+
->willReturn($this->messageManagerMock);
113+
$this->contextMock->expects($this->once())
114+
->method('getRequest')
115+
->willReturn($this->requestMock);
116+
$this->contextMock->expects($this->once())
117+
->method('getResultFactory')
118+
->willReturn($this->resultFactoryMock);
119+
$this->contextMock->expects($this->once())
120+
->method('getSession')
121+
->willReturn($this->session);
122+
123+
$this->lengthValidator = $this->getMockBuilder(\Magento\Framework\Validator\StringLength::class)
124+
->disableOriginalConstructor()
125+
->getMock();
126+
$this->pathValidator =
127+
$this->getMockBuilder(\Magento\MediaStorage\Model\File\Validator\AvailablePath::class)
128+
->disableOriginalConstructor()
129+
->getMock();
130+
$this->fileSystem = $this->createMock(\Magento\Framework\Filesystem::class);
131+
$this->siteMapFactory = $this->createMock(\Magento\Sitemap\Model\SitemapFactory::class);
132+
133+
$this->saveController = new Save(
134+
$this->contextMock,
135+
$this->lengthValidator,
136+
$this->pathValidator,
137+
$this->helper,
138+
$this->fileSystem,
139+
$this->siteMapFactory
90140
);
91141
}
92142

@@ -105,11 +155,8 @@ public function testSaveEmptyDataShouldRedirectToDefault()
105155

106156
public function testTryToSaveInvalidDataShouldFailWithErrors()
107157
{
108-
$validatorClass = \Magento\MediaStorage\Model\File\Validator\AvailablePath::class;
109-
$helperClass = \Magento\Sitemap\Helper\Data::class;
110158
$validPaths = [];
111159
$messages = ['message1', 'message2'];
112-
$sessionClass = \Magento\Backend\Model\Session::class;
113160
$data = ['sitemap_filename' => 'sitemap_filename', 'sitemap_path' => '/sitemap_path'];
114161
$siteMapId = 1;
115162

@@ -121,37 +168,83 @@ public function testTryToSaveInvalidDataShouldFailWithErrors()
121168
->with('sitemap_id')
122169
->willReturn($siteMapId);
123170

124-
$validator = $this->createMock($validatorClass);
125-
$validator->expects($this->once())
171+
$this->pathValidator->expects($this->once())
126172
->method('setPaths')
127173
->with($validPaths)
128174
->willReturnSelf();
129-
$validator->expects($this->once())
175+
$this->pathValidator->expects($this->once())
130176
->method('isValid')
131177
->with('/sitemap_path/sitemap_filename')
132178
->willReturn(false);
133-
$validator->expects($this->once())
179+
$this->pathValidator->expects($this->once())
134180
->method('getMessages')
135181
->willReturn($messages);
136182

137-
$helper = $this->createMock($helperClass);
138-
$helper->expects($this->once())
183+
$this->helper->expects($this->once())
139184
->method('getValidPaths')
140185
->willReturn($validPaths);
141186

142-
$session = $this->createPartialMock($sessionClass, ['setFormData']);
143-
$session->expects($this->once())
187+
$this->session->expects($this->once())
144188
->method('setFormData')
145189
->with($data)
146190
->willReturnSelf();
147191

148-
$this->objectManagerMock->expects($this->once())
149-
->method('create')
150-
->with($validatorClass)
151-
->willReturn($validator);
152-
$this->objectManagerMock->expects($this->any())
153-
->method('get')
154-
->willReturnMap([[$helperClass, $helper], [$sessionClass, $session]]);
192+
$this->messageManagerMock->expects($this->at(0))
193+
->method('addErrorMessage')
194+
->withConsecutive(
195+
[$messages[0]],
196+
[$messages[1]]
197+
)
198+
->willReturnSelf();
199+
200+
$this->resultRedirectMock->expects($this->once())
201+
->method('setPath')
202+
->with('adminhtml/*/edit', ['sitemap_id' => $siteMapId])
203+
->willReturnSelf();
204+
205+
$this->assertSame($this->resultRedirectMock, $this->saveController->execute());
206+
}
207+
208+
public function testTryToSaveInvalidFileNameShouldFailWithErrors()
209+
{
210+
$validPaths = [];
211+
$messages = ['message1', 'message2'];
212+
$data = ['sitemap_filename' => 'sitemap_filename', 'sitemap_path' => '/sitemap_path'];
213+
$siteMapId = 1;
214+
215+
$this->requestMock->expects($this->once())
216+
->method('getPostValue')
217+
->willReturn($data);
218+
$this->requestMock->expects($this->once())
219+
->method('getParam')
220+
->with('sitemap_id')
221+
->willReturn($siteMapId);
222+
223+
$this->lengthValidator->expects($this->once())
224+
->method('isValid')
225+
->with('sitemap_filename')
226+
->willReturn(false);
227+
$this->lengthValidator->expects($this->once())
228+
->method('getMessages')
229+
->willReturn($messages);
230+
231+
$this->pathValidator->expects($this->once())
232+
->method('setPaths')
233+
->with($validPaths)
234+
->willReturnSelf();
235+
$this->pathValidator->expects($this->once())
236+
->method('isValid')
237+
->with('/sitemap_path/sitemap_filename')
238+
->willReturn(true);
239+
240+
$this->helper->expects($this->once())
241+
->method('getValidPaths')
242+
->willReturn($validPaths);
243+
244+
$this->session->expects($this->once())
245+
->method('setFormData')
246+
->with($data)
247+
->willReturnSelf();
155248

156249
$this->messageManagerMock->expects($this->at(0))
157250
->method('addErrorMessage')

0 commit comments

Comments
 (0)