Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContentValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ContentValidatorTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $validator;
16 
20  protected $fileValidatorMock;
21 
25  protected $urlValidatorMock;
26 
30  protected $linkFileMock;
31 
35  protected $sampleFileMock;
36 
37  protected function setUp()
38  {
39  $this->fileValidatorMock = $this->createMock(\Magento\Downloadable\Model\File\ContentValidator::class);
40  $this->urlValidatorMock = $this->createMock(\Magento\Framework\Url\Validator::class);
41  $this->linkFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
42  $this->sampleFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
43  $this->validator = new ContentValidator($this->fileValidatorMock, $this->urlValidatorMock);
44  }
45 
46  public function testIsValid()
47  {
48  $linkFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
49  $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
50  $linkData = [
51  'title' => 'Title',
52  'sort_order' => 1,
53  'price' => 10.1,
54  'shareable' => true,
55  'number_of_downloads' => 100,
56  'link_type' => 'file',
57  'sample_type' => 'file',
58  'link_file_content' => $linkFileContentMock,
59  'sample_file_content' => $sampleFileContentMock,
60  ];
61  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
62  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
63  $linkMock = $this->getLinkMock($linkData);
64  $this->assertTrue($this->validator->isValid($linkMock));
65  }
66 
67  public function testIsValidSkipLinkContent()
68  {
69  $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
70  $linkData = [
71  'title' => 'Title',
72  'sort_order' => 1,
73  'price' => 10.1,
74  'shareable' => true,
75  'number_of_downloads' => 100,
76  'link_type' => 'url',
77  'link_url' => 'http://example.com',
78  'sample_type' => 'file',
79  'sample_file_content' => $sampleFileContentMock,
80  ];
81  $this->fileValidatorMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
82  $this->urlValidatorMock->expects($this->never())->method('isValid')->will($this->returnValue(true));
83  $linkMock = $this->getLinkMock($linkData);
84  $this->assertTrue($this->validator->isValid($linkMock, false));
85  }
86 
87  public function testIsValidSkipSampleContent()
88  {
89  $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
90  $linkData = [
91  'title' => 'Title',
92  'sort_order' => 1,
93  'price' => 10.1,
94  'shareable' => true,
95  'number_of_downloads' => 100,
96  'link_type' => 'url',
97  'link_url' => 'http://example.com',
98  'sample_type' => 'file',
99  'sample_file_content' => $sampleFileContentMock,
100  ];
101  $this->fileValidatorMock->expects($this->never())->method('isValid')->will($this->returnValue(true));
102  $this->urlValidatorMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
103  $linkMock = $this->getLinkMock($linkData);
104  $this->assertTrue($this->validator->isValid($linkMock, true, false));
105  }
106 
114  {
115  $linkContentData = [
116  'title' => 'Title',
117  'sort_order' => $sortOrder,
118  'price' => 10.1,
119  'shareable' => true,
120  'number_of_downloads' => 100,
121  'link_type' => 'file',
122  'sample_type' => 'file',
123  ];
124  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
125  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
126  $contentMock = $this->getLinkMock($linkContentData);
127  $this->validator->isValid($contentMock);
128  }
129 
133  public function getInvalidSortOrder()
134  {
135  return [
136  [-1],
137  ['string'],
138  [1.1],
139  ];
140  }
141 
149  {
150  $linkContentData = [
151  'title' => 'Title',
152  'sort_order' => 1,
153  'price' => $price,
154  'shareable' => true,
155  'number_of_downloads' => 100,
156  'link_type' => 'file',
157  'sample_type' => 'file',
158  ];
159  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
160  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
161  $contentMock = $this->getLinkMock($linkContentData);
162  $this->validator->isValid($contentMock);
163  }
164 
168  public function getInvalidPrice()
169  {
170  return [
171  [-1],
172  ['string'],
173  ];
174  }
175 
182  public function testIsValidThrowsExceptionIfNumberOfDownloadsIsInvalid($numberOfDownloads)
183  {
184  $linkContentData = [
185  'title' => 'Title',
186  'sort_order' => 1,
187  'price' => 10.5,
188  'shareable' => true,
189  'number_of_downloads' => $numberOfDownloads,
190  'link_type' => 'file',
191  'sample_type' => 'file',
192  ];
193  $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
194  $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
195  $contentMock = $this->getLinkMock($linkContentData);
196  $this->validator->isValid($contentMock);
197  }
198 
202  public function getInvalidNumberOfDownloads()
203  {
204  return [
205  [-1],
206  [2.71828],
207  ['string'],
208  ];
209  }
210 
215  protected function getLinkMock(array $linkData)
216  {
217  $linkMock = $this->getMockBuilder(\Magento\Downloadable\Api\Data\LinkInterface::class)
218  ->setMethods(
219  [
220  'getTitle',
221  'getPrice',
222  'getSortOrder',
223  'isShareable',
224  'getNumberOfDownloads',
225  'getLinkType',
226  'getLinkFile'
227  ]
228  )
229  ->getMockForAbstractClass();
230  $linkMock->expects($this->any())->method('getTitle')->will($this->returnValue(
231  $linkData['title']
232  ));
233  $linkMock->expects($this->any())->method('getPrice')->will($this->returnValue(
234  $linkData['price']
235  ));
236  $linkMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
237  $linkData['sort_order']
238  ));
239  $linkMock->expects($this->any())->method('isShareable')->will($this->returnValue(
240  $linkData['shareable']
241  ));
242  $linkMock->expects($this->any())->method('getNumberOfDownloads')->will($this->returnValue(
243  $linkData['number_of_downloads']
244  ));
245  $linkMock->expects($this->any())->method('getLinkType')->will($this->returnValue(
246  $linkData['link_type']
247  ));
248  $linkMock->expects($this->any())->method('getLinkFile')->will($this->returnValue(
249  $this->linkFileMock
250  ));
251  if (isset($linkData['link_url'])) {
252  $linkMock->expects($this->any())->method('getLinkUrl')->will($this->returnValue(
253  $linkData['link_url']
254  ));
255  }
256  if (isset($linkData['sample_url'])) {
257  $linkMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
258  $linkData['sample_url']
259  ));
260  }
261  if (isset($linkData['sample_type'])) {
262  $linkMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
263  $linkData['sample_type']
264  ));
265  }
266  if (isset($linkData['link_file_content'])) {
267  $linkMock->expects($this->any())->method('getLinkFileContent')->willReturn($linkData['link_file_content']);
268  }
269  if (isset($linkData['sample_file_content'])) {
270  $linkMock->expects($this->any())->method('getSampleFileContent')
271  ->willReturn($linkData['sample_file_content']);
272  }
273  $linkMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
274  $this->sampleFileMock
275  ));
276  return $linkMock;
277  }
278 }
$price