Skip to content

Commit 0995b37

Browse files
author
Allan Paiste
committed
Increased test-coverage
1 parent b9419fb commit 0995b37

File tree

3 files changed

+248
-25
lines changed

3 files changed

+248
-25
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category\Image;
7+
8+
use \Magento\Catalog\Controller\Adminhtml\Category\Image\Upload as Model;
9+
use \Magento\Framework\App\Request\Http as Request;
10+
11+
/**
12+
* Class UploadTest
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
15+
class UploadTest extends \PHPUnit_Framework_TestCase
16+
{
17+
protected $objectManager;
18+
19+
protected function setUp()
20+
{
21+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
22+
}
23+
24+
public function uploadedImageNameProvider()
25+
{
26+
return [
27+
['image1', 'image1'],
28+
['image2', 'image2'],
29+
[null, 'image'],
30+
];
31+
}
32+
33+
/**
34+
* @param $name
35+
* @param $savedName
36+
*
37+
* @dataProvider uploadedImageNameProvider
38+
*/
39+
public function testExecuteShouldSaveUploadedImageWithSpecifiedNameToTmpFolder($name, $savedName)
40+
{
41+
$request = $this->objectManager->getObject(Request::class);
42+
43+
$uploader = $this->getMock(
44+
\Magento\Catalog\Model\ImageUploader::class, ['saveFileToTmpDir'], [], '', false);
45+
46+
$resultFactory = $this->getMock(
47+
\Magento\Framework\Controller\ResultFactory::class, ['create'], [], '', false);
48+
49+
$resultFactory->expects($this->once())
50+
->method('create')
51+
->will($this->returnValue(new \Magento\Framework\DataObject()));
52+
53+
$model = $this->objectManager->getObject(Model::class, [
54+
'context' => $this->objectManager->getObject(\Magento\Backend\App\Action\Context::class, [
55+
'request' => $request,
56+
'resultFactory' => $resultFactory
57+
]),
58+
'imageUploader' => $uploader
59+
]);
60+
61+
$uploader->expects($this->once())
62+
->method('saveFileToTmpDir')
63+
->with($savedName)
64+
->will($this->returnValue([]));
65+
66+
$request->setParam('param_name', $name);
67+
68+
$model->execute();
69+
}
70+
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class SaveTest extends \PHPUnit_Framework_TestCase
8484
*/
8585
protected function setUp()
8686
{
87-
$this->markTestSkipped('Due to MAGETWO-48956');
8887
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
8988

9089
$this->contextMock = $this->getMock(
@@ -201,6 +200,8 @@ protected function setUp()
201200
*/
202201
public function testExecute($categoryId, $storeId, $parentId)
203202
{
203+
$this->markTestSkipped('Due to MAGETWO-48956');
204+
204205
$rootCategoryId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
205206
$products = [['any_product']];
206207
$postData = [
@@ -577,4 +578,84 @@ public function dataProviderExecute()
577578
]
578579
];
579580
}
581+
582+
public function attributeValueDataProvider()
583+
{
584+
return [
585+
[['attribute1' => null, 'attribute2' => 123]],
586+
[['attribute2' => 123]]
587+
];
588+
}
589+
590+
/**
591+
* @dataProvider attributeValueDataProvider
592+
*
593+
* @param $data
594+
*/
595+
public function testImagePreprocessingShouldSetAttributesWithImageBackendToFalse($data)
596+
{
597+
$eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityType'], [], '', false);
598+
599+
$collection = new \Magento\Framework\DataObject(['attribute_collection' => [
600+
new \Magento\Framework\DataObject([
601+
'attribute_code' => 'attribute1',
602+
'backend' => $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class)
603+
]),
604+
new \Magento\Framework\DataObject([
605+
'attribute_code' => 'attribute2',
606+
'backend' => new \Magento\Framework\DataObject()
607+
])
608+
]]);
609+
610+
$eavConfig->expects($this->once())
611+
->method('getEntityType')
612+
->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
613+
->will($this->returnValue($collection));
614+
615+
$model = $this->objectManager->getObject(\Magento\Catalog\Controller\Adminhtml\Category\Save::class, [
616+
'eavConfig' => $eavConfig
617+
]);
618+
619+
$result = $model->imagePreprocessing($data);
620+
621+
$this->assertEquals([
622+
'attribute1' => false,
623+
'attribute2' => 123
624+
], $result);
625+
}
626+
627+
public function testImagePreprocessingShouldNotSetValueToFalseWhenValueSet()
628+
{
629+
$eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityType'], [], '', false);
630+
631+
$collection = new \Magento\Framework\DataObject(['attribute_collection' => [
632+
new \Magento\Framework\DataObject([
633+
'attribute_code' => 'attribute1',
634+
'backend' => $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class)
635+
]),
636+
new \Magento\Framework\DataObject([
637+
'attribute_code' => 'attribute2',
638+
'backend' => new \Magento\Framework\DataObject()
639+
])
640+
]]);
641+
642+
$eavConfig->expects($this->once())
643+
->method('getEntityType')
644+
->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
645+
->will($this->returnValue($collection));
646+
647+
$model = $this->objectManager->getObject(\Magento\Catalog\Controller\Adminhtml\Category\Save::class, [
648+
'eavConfig' => $eavConfig
649+
]);
650+
651+
$result = $model->imagePreprocessing([
652+
'attribute1' => 'somevalue',
653+
'attribute2' => null
654+
]);
655+
656+
$this->assertEquals([
657+
'attribute1' => 'somevalue',
658+
'attribute2' => null
659+
], $result);
660+
}
580661
}

app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\Catalog\Test\Unit\Model;
1010

1111
use Magento\Catalog\Model\Indexer;
12+
use Magento\Catalog\Model\Category as Model;
1213

1314
/**
1415
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -89,9 +90,16 @@ class CategoryTest extends \PHPUnit_Framework_TestCase
8990
*/
9091
protected $attributeValueFactory;
9192

93+
/**
94+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
95+
*/
96+
protected $objectManager;
97+
9298
protected function setUp()
9399
{
94-
$this->context = $this->getMock(
100+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
101+
102+
$this->context = $this->getMock(
95103
\Magento\Framework\Model\Context::class,
96104
['getEventDispatcher', 'getCacheManager'],
97105
[],
@@ -108,37 +116,37 @@ protected function setUp()
108116

109117
$this->registry = $this->getMock(\Magento\Framework\Registry::class);
110118
$this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
111-
$this->categoryTreeResource = $this->getMock(
112-
\Magento\Catalog\Model\ResourceModel\Category\Tree::class,
113-
[],
114-
[],
115-
'',
116-
false
119+
$this->categoryTreeResource = $this->getMock(
120+
\Magento\Catalog\Model\ResourceModel\Category\Tree::class,
121+
[],
122+
[],
123+
'',
124+
false
117125
);
118-
$this->categoryTreeFactory = $this->getMock(
126+
$this->categoryTreeFactory = $this->getMock(
119127
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory::class,
120128
['create'],
121129
[],
122130
'',
123131
false);
124132
$this->categoryRepository = $this->getMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
125-
$this->storeCollectionFactory = $this->getMock(
133+
$this->storeCollectionFactory = $this->getMock(
126134
\Magento\Store\Model\ResourceModel\Store\CollectionFactory::class,
127135
['create'],
128136
[],
129137
'',
130138
false
131139
);
132140
$this->url = $this->getMock(\Magento\Framework\UrlInterface::class);
133-
$this->productCollectionFactory = $this->getMock(
141+
$this->productCollectionFactory = $this->getMock(
134142
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
135143
['create'],
136144
[],
137145
'',
138146
false
139147
);
140148
$this->catalogConfig = $this->getMock(\Magento\Catalog\Model\Config::class, [], [], '', false);
141-
$this->filterManager = $this->getMock(
149+
$this->filterManager = $this->getMock(
142150
\Magento\Framework\Filter\FilterManager::class,
143151
['translitUrl'],
144152
[],
@@ -148,27 +156,27 @@ protected function setUp()
148156
$this->flatState = $this->getMock(\Magento\Catalog\Model\Indexer\Category\Flat\State::class, [], [], '', false);
149157
$this->flatIndexer = $this->getMock(\Magento\Framework\Indexer\IndexerInterface::class);
150158
$this->productIndexer = $this->getMock(\Magento\Framework\Indexer\IndexerInterface::class);
151-
$this->categoryUrlPathGenerator = $this->getMock(
159+
$this->categoryUrlPathGenerator = $this->getMock(
152160
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class,
153161
[],
154162
[],
155163
'',
156164
false
157165
);
158166
$this->urlFinder = $this->getMock(\Magento\UrlRewrite\Model\UrlFinderInterface::class);
159-
$this->resource = $this->getMock(
167+
$this->resource = $this->getMock(
160168
\Magento\Catalog\Model\ResourceModel\Category::class,
161169
[],
162170
[],
163171
'',
164172
false
165173
);
166-
$this->indexerRegistry = $this->getMock(
167-
\Magento\Framework\Indexer\IndexerRegistry::class,
168-
['get'],
169-
[],
170-
'',
171-
false
174+
$this->indexerRegistry = $this->getMock(
175+
\Magento\Framework\Indexer\IndexerRegistry::class,
176+
['get'],
177+
[],
178+
'',
179+
false
172180
);
173181

174182
$this->metadataServiceMock = $this->getMock(\Magento\Catalog\Api\CategoryAttributeRepositoryInterface::class);
@@ -198,7 +206,7 @@ public function testFormatUrlKey()
198206
public function testMoveWhenCannotFindParentCategory()
199207
{
200208
$this->markTestIncomplete('MAGETWO-31165');
201-
$parentCategory = $this->getMock(
209+
$parentCategory = $this->getMock(
202210
\Magento\Catalog\Model\Category::class,
203211
['getId', 'setStoreId', 'load'],
204212
[],
@@ -223,7 +231,7 @@ public function testMoveWhenCannotFindParentCategory()
223231
*/
224232
public function testMoveWhenCannotFindNewCategory()
225233
{
226-
$parentCategory = $this->getMock(
234+
$parentCategory = $this->getMock(
227235
\Magento\Catalog\Model\Category::class,
228236
['getId', 'setStoreId', 'load'],
229237
[],
@@ -250,7 +258,7 @@ public function testMoveWhenCannotFindNewCategory()
250258
public function testMoveWhenParentCategoryIsSameAsChildCategory()
251259
{
252260
$this->markTestIncomplete('MAGETWO-31165');
253-
$parentCategory = $this->getMock(
261+
$parentCategory = $this->getMock(
254262
\Magento\Catalog\Model\Category::class,
255263
['getId', 'setStoreId', 'load'],
256264
[],
@@ -277,7 +285,7 @@ public function testMovePrimaryWorkflow()
277285
->method('get')
278286
->with('catalog_category_product')
279287
->will($this->returnValue($indexer));
280-
$parentCategory = $this->getMock(
288+
$parentCategory = $this->getMock(
281289
\Magento\Catalog\Model\Category::class,
282290
['getId', 'setStoreId', 'load'],
283291
[],
@@ -313,7 +321,7 @@ public function testGetUseFlatResourceTrue()
313321

314322
protected function getCategoryModel()
315323
{
316-
return (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
324+
return $this->objectManager->getObject(
317325
\Magento\Catalog\Model\Category::class,
318326
[
319327
'context' => $this->context,
@@ -487,4 +495,68 @@ public function testGetCustomAttributes()
487495
$this->category->getCustomAttribute($descriptionAttributeCode)->getValue()
488496
);
489497
}
498+
499+
public function imageAttributeNameAndUrlProvider()
500+
{
501+
return [
502+
['testimage', 'http://www.test123.com/catalog/category/testimage'],
503+
[false, false]
504+
];
505+
}
506+
507+
/**
508+
* @param $value
509+
* @param $url
510+
*
511+
* @dataProvider imageAttributeNameAndUrlProvider
512+
*/
513+
public function testGetImageUrlShouldGenerateMediaUrlForSpecifiedAttributeValue($value, $url)
514+
{
515+
$storeManager = $this->getMock(\Magento\Store\Model\StoreManager::class, ['getStore'], [], '', false);
516+
$store = $this->getMock(\Magento\Store\Model\Store::class, ['getBaseUrl'], [], '', false);
517+
518+
$storeManager->expects($this->any())
519+
->method('getStore')
520+
->will($this->returnValue($store));
521+
522+
$store->expects($this->any())
523+
->method('getBaseUrl')
524+
->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
525+
->will($this->returnValue('http://www.test123.com/'));
526+
527+
$model = $this->objectManager->getObject(Model::class, [
528+
'storeManager' => $storeManager
529+
]);
530+
531+
$model->setData('attribute1', $value);
532+
533+
$result = $model->getImageUrl('attribute1');
534+
535+
$this->assertEquals($url, $result);
536+
}
537+
538+
public function testGetImageUrlShouldGenerateMediaUrlForImageAttributeValue()
539+
{
540+
$storeManager = $this->getMock(\Magento\Store\Model\StoreManager::class, ['getStore'], [], '', false);
541+
$store = $this->getMock(\Magento\Store\Model\Store::class, ['getBaseUrl'], [], '', false);
542+
543+
$storeManager->expects($this->any())
544+
->method('getStore')
545+
->will($this->returnValue($store));
546+
547+
$store->expects($this->any())
548+
->method('getBaseUrl')
549+
->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
550+
->will($this->returnValue('http://www.test123.com/'));
551+
552+
$model = $this->objectManager->getObject(Model::class, [
553+
'storeManager' => $storeManager
554+
]);
555+
556+
$model->setData('image', 'myimage');
557+
558+
$result = $model->getImageUrl();
559+
560+
$this->assertEquals('http://www.test123.com/catalog/category/myimage', $result);
561+
}
490562
}

0 commit comments

Comments
 (0)