3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Block \Adminhtml \Category \Checkboxes ;
7
9
10
+ use Magento \Catalog \Helper \DefaultCategory ;
11
+ use Magento \Framework \ObjectManagerInterface ;
12
+ use Magento \Framework \Serialize \SerializerInterface ;
13
+ use Magento \Framework \View \LayoutInterface ;
14
+ use Magento \TestFramework \Helper \Bootstrap ;
15
+ use PHPUnit \Framework \TestCase ;
16
+
8
17
/**
18
+ * Checks category chooser block behaviour
19
+ *
20
+ * @see \Magento\Catalog\Block\Adminhtml\Category\Checkboxes\Tree
21
+ *
9
22
* @magentoAppArea adminhtml
10
23
* @magentoDbIsolation enabled
11
24
* @magentoAppIsolation enabled
12
25
*/
13
- class TreeTest extends \ PHPUnit \ Framework \ TestCase
26
+ class TreeTest extends TestCase
14
27
{
15
- /** @var \Magento\Catalog\Block\Adminhtml\Category\Checkboxes\Tree */
16
- protected $ block ;
28
+ /** @var ObjectManagerInterface */
29
+ private $ objectManager ;
30
+
31
+ /** @var Tree */
32
+ private $ block ;
33
+
34
+ /** @var SerializerInterface */
35
+ private $ json ;
17
36
37
+ /** @var DefaultCategory */
38
+ private $ defaultCategoryHelper ;
39
+
40
+ /**
41
+ * @inheritdoc
42
+ */
18
43
protected function setUp (): void
19
44
{
20
- $ this ->block = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
21
- \Magento \Catalog \Block \Adminhtml \Category \Checkboxes \Tree::class
22
- );
45
+ $ this ->objectManager = Bootstrap::getObjectManager ();
46
+ $ this ->block = $ this ->objectManager ->get (LayoutInterface::class)->createBlock (Tree::class);
47
+ $ this ->json = $ this ->objectManager ->get (SerializerInterface::class);
48
+ $ this ->defaultCategoryHelper = $ this ->objectManager ->get (DefaultCategory::class);
23
49
}
24
50
25
- public function testSetGetCategoryIds ()
51
+ /**
52
+ * @return void
53
+ */
54
+ public function testSetGetCategoryIds (): void
26
55
{
27
56
$ this ->block ->setCategoryIds ([1 , 4 , 7 , 56 , 2 ]);
28
57
$ this ->assertEquals ([1 , 4 , 7 , 56 , 2 ], $ this ->block ->getCategoryIds ());
29
58
}
30
59
31
60
/**
32
61
* @magentoDataFixture Magento/Catalog/_files/categories.php
62
+ *
63
+ * @return void
33
64
*/
34
- public function testGetTreeJson ()
65
+ public function testGetTreeJson (): void
35
66
{
36
67
$ jsonTree = $ this ->block ->getTreeJson ();
37
68
$ this ->assertStringContainsString ('Default Category (4) ' , $ jsonTree );
@@ -45,4 +76,17 @@ public function testGetTreeJson()
45
76
$ this ->assertStringContainsString ('Category 12 (2) ' , $ jsonTree );
46
77
$ this ->assertStringMatchesFormat ('%s"path":"1\/2\/%s\/%s\/%s"%s ' , $ jsonTree );
47
78
}
79
+
80
+ /**
81
+ * @return void
82
+ */
83
+ public function testGetTreeJsonWithSelectedCategory (): void
84
+ {
85
+ $ this ->block ->setCategoryIds ($ this ->defaultCategoryHelper ->getId ());
86
+ $ result = $ this ->json ->unserialize ($ this ->block ->getTreeJson ());
87
+ $ item = reset ($ result );
88
+ $ this ->assertNotEmpty ($ item );
89
+ $ this ->assertStringContainsString ('Default Category ' , $ item ['text ' ]);
90
+ $ this ->assertTrue ($ item ['checked ' ]);
91
+ }
48
92
}
0 commit comments