8
8
namespace Magento \CmsUrlRewrite \Plugin \Cms \Model \Store ;
9
9
10
10
use Magento \Framework \ObjectManagerInterface ;
11
- use Magento \Framework \Registry ;
12
11
use Magento \Store \Model \Store ;
13
12
use Magento \Store \Model \StoreFactory ;
14
- use Magento \Store \Api \WebsiteRepositoryInterface ;
15
13
use Magento \TestFramework \Helper \Bootstrap ;
16
14
use Magento \UrlRewrite \Model \UrlFinderInterface ;
17
15
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
@@ -34,20 +32,10 @@ class ViewTest extends \PHPUnit\Framework\TestCase
34
32
private $ objectManager ;
35
33
36
34
/**
37
- * @var StoreFactory
35
+ * @var Store
38
36
*/
39
37
private $ storeFactory ;
40
38
41
- /**
42
- * @var string
43
- */
44
- private $ storeCode ;
45
-
46
- /**
47
- * @var WebsiteRepositoryInterface
48
- */
49
- private $ websiteRepository ;
50
-
51
39
/**
52
40
* @inheritdoc
53
41
*/
@@ -56,8 +44,6 @@ protected function setUp()
56
44
$ this ->objectManager = Bootstrap::getObjectManager ();
57
45
$ this ->urlFinder = $ this ->objectManager ->create (UrlFinderInterface::class);
58
46
$ this ->storeFactory = $ this ->objectManager ->create (StoreFactory::class);
59
- $ this ->websiteRepository = $ this ->objectManager ->get (WebsiteRepositoryInterface::class);
60
- $ this ->storeCode = 'test_ ' . random_int (0 , 999 );
61
47
}
62
48
63
49
/**
@@ -66,86 +52,57 @@ protected function setUp()
66
52
* @magentoDataFixture Magento/Cms/_files/pages.php
67
53
*/
68
54
public function testUrlRewritesChangesAfterStoreSave ()
55
+ {
56
+ $ storeId = $ this ->createStore ();
57
+ $ this ->assertUrlRewritesCount ($ storeId , 1 );
58
+ $ this ->deleteStore ($ storeId );
59
+ $ this ->assertUrlRewritesCount ($ storeId , 0 );
60
+ }
61
+
62
+ /**
63
+ * Assert url rewrites count by store id
64
+ *
65
+ * @param int $storeId
66
+ * @param int $expectedCount
67
+ */
68
+ private function assertUrlRewritesCount (int $ storeId , int $ expectedCount ): void
69
69
{
70
70
$ data = [
71
71
UrlRewrite::REQUEST_PATH => 'page100 ' ,
72
+ UrlRewrite::STORE_ID => $ storeId
72
73
];
73
74
$ urlRewrites = $ this ->urlFinder ->findAllByData ($ data );
74
- $ this ->assertCount (1 , $ urlRewrites );
75
- $ this ->createStore ();
76
- $ urlRewrites = $ this ->urlFinder ->findAllByData ($ data );
77
- $ this ->assertCount (2 , $ urlRewrites );
78
- $ this ->deleteStore ();
79
- $ urlRewrites = $ this ->urlFinder ->findAllByData ($ data );
80
- $ this ->assertCount (1 , $ urlRewrites );
75
+ $ this ->assertCount ($ expectedCount , $ urlRewrites );
81
76
}
82
77
83
78
/**
84
79
* Create test store
85
80
*
86
- * @return void
81
+ * @return int
87
82
*/
88
- private function createStore (): void
83
+ private function createStore (): int
89
84
{
90
- /** @var $store Store */
91
85
$ store = $ this ->storeFactory ->create ();
92
- if (!$ store ->load ($ this ->storeCode , 'code ' )->getId ()) {
93
- $ store ->setData (
94
- [
95
- 'code ' => $ this ->storeCode ,
96
- 'website_id ' => $ this ->websiteRepository ->getDefault ()->getId (),
97
- 'group_id ' => $ this ->websiteRepository ->getDefault ()->getDefaultGroupId (),
98
- 'name ' => 'Test Store ' ,
99
- 'sort_order ' => '0 ' ,
100
- 'is_active ' => '1 ' ,
101
- ]
102
- );
103
- $ store ->save ();
104
- } else {
105
- if ($ store ->getId ()) {
106
- /** @var \Magento\TestFramework\Helper\Bootstrap $registry */
107
- $ registry = $ this ->objectManager ->get (
108
- Registry::class
109
- );
110
- $ registry ->unregister ('isSecureArea ' );
111
- $ registry ->register ('isSecureArea ' , true );
112
- $ store ->delete ();
113
- $ registry ->unregister ('isSecureArea ' );
114
- $ registry ->register ('isSecureArea ' , false );
115
- $ store = $ this ->objectManager ->create (Store::class);
116
- $ store ->setData (
117
- [
118
- 'code ' => $ this ->storeCode ,
119
- 'website_id ' => $ this ->websiteRepository ->getDefault ()->getId (),
120
- 'group_id ' => $ this ->websiteRepository ->getDefault ()->getDefaultGroupId (),
121
- 'name ' => 'Test Store ' ,
122
- 'sort_order ' => '0 ' ,
123
- 'is_active ' => '1 ' ,
124
- ]
125
- );
126
- $ store ->save ();
127
- }
128
- }
86
+ $ store ->setCode ('test_ ' . random_int (0 , 999 ))
87
+ ->setName ('Test Store ' )
88
+ ->unsId ()
89
+ ->save ();
90
+
91
+ return (int )$ store ->getId ();
129
92
}
130
93
131
94
/**
132
95
* Delete test store
133
96
*
97
+ * @param int $storeId
134
98
* @return void
135
99
*/
136
- private function deleteStore (): void
100
+ private function deleteStore (int $ storeId ): void
137
101
{
138
- /** @var Registry $registry */
139
- $ registry = $ this ->objectManager ->get (Registry::class);
140
- $ registry ->unregister ('isSecureArea ' );
141
- $ registry ->register ('isSecureArea ' , true );
142
- /** @var Store $store */
143
- $ store = $ this ->objectManager ->get (Store::class);
144
- $ store ->load ($ this ->storeCode , 'code ' );
145
- if ($ store ->getId ()) {
102
+ $ store = $ this ->storeFactory ->create ();
103
+ $ store ->load ($ storeId );
104
+ if ($ store !== null ) {
146
105
$ store ->delete ();
147
106
}
148
- $ registry ->unregister ('isSecureArea ' );
149
- $ registry ->register ('isSecureArea ' , false );
150
107
}
151
108
}
0 commit comments