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 \Cms \Api ;
7
9
8
10
use Magento \Authorization \Model \Role ;
@@ -191,15 +193,12 @@ public function testGet(): void
191
193
*/
192
194
public function testGetByStores (string $ requestStore ): void
193
195
{
194
- $ page = $ this ->getPageByIdentifier ->execute ('page100 ' , 0 );
195
- $ storeCode = $ requestStore == 'all ' ? 'admin ' : $ requestStore ;
196
- $ store = $ this ->storeManager ->getStore ($ storeCode );
197
- $ this ->updatePage ($ page , ['store_id ' => $ store ->getId ()]);
198
- $ page = $ this ->getPageByIdentifier ->execute ('page100 ' , $ store ->getId ());
199
- $ comparedFields = $ this ->getPageRequestData ()['page ' ];
196
+ $ newStoreId = $ this ->getStoreIdByRequestStore ($ requestStore );
197
+ $ this ->updatePage ('page100 ' , 0 , ['store_id ' => $ newStoreId ]);
198
+ $ page = $ this ->getPageByIdentifier ->execute ('page100 ' , $ newStoreId );
200
199
$ expectedData = array_intersect_key (
201
200
$ this ->dataObjectProcessor ->buildOutputDataArray ($ page , PageInterface::class),
202
- $ comparedFields
201
+ $ this -> getPageRequestData ()[ ' page ' ]
203
202
);
204
203
$ serviceInfo = $ this ->getServiceInfo (
205
204
'GetById ' ,
@@ -212,9 +211,7 @@ public function testGetByStores(string $requestStore): void
212
211
}
213
212
214
213
$ page = $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ requestStore );
215
- $ this ->assertNotNull ($ page ['id ' ]);
216
- $ actualData = array_intersect_key ($ page , $ comparedFields );
217
- $ this ->assertEquals ($ expectedData , $ actualData , 'Error while getting page. ' );
214
+ $ this ->assertResponseData ($ page , $ expectedData );
218
215
}
219
216
220
217
/**
@@ -255,27 +252,17 @@ public function testCreate(): void
255
252
*/
256
253
public function testCreateByStores (string $ requestStore ): void
257
254
{
255
+ $ newStoreId = $ this ->getStoreIdByRequestStore ($ requestStore );
258
256
$ serviceInfo = $ this ->getServiceInfo ('Save ' , Request::HTTP_METHOD_POST );
259
257
$ requestData = $ this ->getPageRequestData ();
258
+
260
259
$ page = $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ requestStore );
261
- $ this ->assertNotNull ($ page ['id ' ]);
262
- $ storeCode = $ requestStore == 'all ' ? 'admin ' : $ requestStore ;
263
- $ store = $ this ->storeManager ->getStore ($ storeCode );
264
- $ this ->currentPage = $ this ->getPageByIdentifier ->execute (
265
- $ requestData ['page ' ][PageInterface::IDENTIFIER ],
266
- $ store ->getId ()
267
- );
268
- $ actualData = array_intersect_key ($ page , $ requestData ['page ' ]);
269
- $ this ->assertEquals ($ requestData ['page ' ], $ actualData , 'The page was saved with an error. ' );
270
- if ($ requestStore != 'all ' ) {
271
- $ this ->cmsUiDataProvider ->addFilter (
272
- $ this ->filterBuilder ->setField ('store_id ' )->setValue ($ store ->getId ())->create ()
273
- );
274
- }
275
- $ pageGridData = $ this ->cmsUiDataProvider ->getData ();
260
+ $ this ->currentPage = $ this ->getPageByIdentifier ($ requestData ['page ' ][PageInterface::IDENTIFIER ], $ newStoreId );
261
+ $ this ->assertResponseData ($ page , $ requestData ['page ' ]);
262
+ $ pageGridData = $ this ->getPageGridDataByStoreCode ($ requestStore );
276
263
$ this ->assertTrue (
277
264
$ this ->isPageInArray ($ pageGridData ['items ' ], $ page ['id ' ]),
278
- sprintf ('The "%s" page is missing from the "%s" store ' , $ page ['title ' ], $ storeCode )
265
+ sprintf ('The "%s" page is missing from the "%s" store ' , $ page ['title ' ], $ requestStore )
279
266
);
280
267
}
281
268
@@ -373,10 +360,8 @@ public function testUpdateOneField(): void
373
360
*/
374
361
public function testUpdateByStores (string $ requestStore ): void
375
362
{
376
- $ page = $ this ->getPageByIdentifier ->execute ('page100 ' , 0 );
377
- $ storeCode = $ requestStore == 'all ' ? 'admin ' : $ requestStore ;
378
- $ store = $ this ->storeManager ->getStore ($ storeCode );
379
- $ this ->updatePage ($ page , ['store_id ' => $ store ->getId ()]);
363
+ $ newStoreId = $ this ->getStoreIdByRequestStore ($ requestStore );
364
+ $ page = $ this ->updatePage ('page100 ' , 0 , ['store_id ' => $ newStoreId ]);
380
365
$ serviceInfo = $ this ->getServiceInfo (
381
366
'Save ' ,
382
367
Request::HTTP_METHOD_PUT ,
@@ -385,22 +370,12 @@ public function testUpdateByStores(string $requestStore): void
385
370
$ requestData = $ this ->getPageRequestData ();
386
371
387
372
$ page = $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ requestStore );
388
- $ this ->assertNotNull ($ page ['id ' ]);
389
- $ this ->currentPage = $ this ->getPageByIdentifier ->execute (
390
- $ requestData ['page ' ][PageInterface::IDENTIFIER ],
391
- $ store ->getId ()
392
- );
393
- $ actualData = array_intersect_key ($ page , $ requestData ['page ' ]);
394
- $ this ->assertEquals ($ requestData ['page ' ], $ actualData , 'The page was saved with an error. ' );
395
- if ($ requestStore != 'all ' ) {
396
- $ this ->cmsUiDataProvider ->addFilter (
397
- $ this ->filterBuilder ->setField ('store_id ' )->setValue ($ store ->getId ())->create ()
398
- );
399
- }
400
- $ pageGridData = $ this ->cmsUiDataProvider ->getData ();
373
+ $ this ->currentPage = $ this ->getPageByIdentifier ($ requestData ['page ' ][PageInterface::IDENTIFIER ], $ newStoreId );
374
+ $ this ->assertResponseData ($ page , $ requestData ['page ' ]);
375
+ $ pageGridData = $ this ->getPageGridDataByStoreCode ($ requestStore );
401
376
$ this ->assertTrue (
402
377
$ this ->isPageInArray ($ pageGridData ['items ' ], $ page ['id ' ]),
403
- sprintf ('The "%s" page is missing from the "%s" store ' , $ page ['title ' ], $ storeCode )
378
+ sprintf ('The "%s" page is missing from the "%s" store ' , $ page ['title ' ], $ requestStore )
404
379
);
405
380
}
406
381
@@ -440,10 +415,8 @@ public function testDelete(): void
440
415
*/
441
416
public function testDeleteByStores (string $ requestStore ): void
442
417
{
443
- $ page = $ this ->getPageByIdentifier ->execute ('page100 ' , 0 );
444
- $ storeCode = $ requestStore == 'all ' ? 'admin ' : $ requestStore ;
445
- $ store = $ this ->storeManager ->getStore ($ storeCode );
446
- $ this ->updatePage ($ page , ['store_id ' => $ store ->getId ()]);
418
+ $ newStoreId = $ this ->getStoreIdByRequestStore ($ requestStore );
419
+ $ page = $ this ->updatePage ('page100 ' , 0 , ['store_id ' => $ newStoreId ]);
447
420
$ serviceInfo = $ this ->getServiceInfo (
448
421
'DeleteById ' ,
449
422
Request::HTTP_METHOD_DELETE ,
@@ -453,17 +426,13 @@ public function testDeleteByStores(string $requestStore): void
453
426
if (TESTS_WEB_API_ADAPTER === self ::ADAPTER_SOAP ) {
454
427
$ requestData [PageInterface::PAGE_ID ] = $ page ->getId ();
455
428
}
429
+
456
430
$ pageResponse = $ this ->_webApiCall ($ serviceInfo , $ requestData , null , $ requestStore );
457
431
$ this ->assertTrue ($ pageResponse );
458
- if ($ requestStore != 'all ' ) {
459
- $ this ->cmsUiDataProvider ->addFilter (
460
- $ this ->filterBuilder ->setField ('store_id ' )->setValue ($ store ->getId ())->create ()
461
- );
462
- }
463
- $ pageGridData = $ this ->cmsUiDataProvider ->getData ();
432
+ $ pageGridData = $ this ->getPageGridDataByStoreCode ($ requestStore );
464
433
$ this ->assertFalse (
465
- $ this ->isPageInArray ($ pageGridData ['items ' ], $ page ->getId ()),
466
- sprintf ('The "%s" page should not be present on the "%s" store ' , $ page ->getTitle (), $ storeCode )
434
+ $ this ->isPageInArray ($ pageGridData ['items ' ], ( int ) $ page ->getId ()),
435
+ sprintf ('The "%s" page should not be present on the "%s" store ' , $ page ->getTitle (), $ requestStore )
467
436
);
468
437
}
469
438
@@ -561,12 +530,12 @@ public function byStoresProvider(): array
561
530
'default_store ' => [
562
531
'request_store ' => 'default ' ,
563
532
],
564
- /* 'second_store' => [
533
+ 'second_store ' => [
565
534
'request_store ' => 'fixture_second_store ' ,
566
535
],
567
536
'all ' => [
568
537
'request_store ' => 'all ' ,
569
- ],*/
538
+ ],
570
539
];
571
540
}
572
541
@@ -606,9 +575,9 @@ private function prepareCmsPages(): array
606
575
/**
607
576
* Create page with hard-coded identifier to test with create-delete-create flow.
608
577
* @param string $identifier
609
- * @return string
578
+ * @return int
610
579
*/
611
- private function createPageWithIdentifier ($ identifier ): string
580
+ private function createPageWithIdentifier ($ identifier ): int
612
581
{
613
582
$ serviceInfo = $ this ->getServiceInfo ('Save ' , Request::HTTP_METHOD_POST );
614
583
$ requestData = [
@@ -792,12 +761,14 @@ private function isPageInArray(array $pageGridData, int $pageId): bool
792
761
/**
793
762
* Update page with data
794
763
*
795
- * @param PageInterface $page
764
+ * @param string $pageIdentifier
765
+ * @param int $storeId
796
766
* @param array $pageData
797
767
* @return PageInterface
798
768
*/
799
- private function updatePage (PageInterface $ page , array $ pageData ): PageInterface
769
+ private function updatePage (string $ pageIdentifier , int $ storeId , array $ pageData ): PageInterface
800
770
{
771
+ $ page = $ this ->getPageByIdentifier ->execute ($ pageIdentifier , $ storeId );
801
772
$ page ->addData ($ pageData );
802
773
803
774
return $ this ->pageRepository ->save ($ page );
@@ -820,4 +791,68 @@ private function getPageRequestData(): array
820
791
]
821
792
];
822
793
}
794
+
795
+ /**
796
+ * Get store id by request store code
797
+ *
798
+ * @param string $requestStoreCode
799
+ * @return int
800
+ */
801
+ private function getStoreIdByRequestStore (string $ requestStoreCode ): int
802
+ {
803
+ $ storeCode = $ requestStoreCode === 'all ' ? 'admin ' : $ requestStoreCode ;
804
+ $ store = $ this ->storeManager ->getStore ($ storeCode );
805
+
806
+ return (int )$ store ->getId ();
807
+ }
808
+
809
+ /**
810
+ * Check that the response data is as expected
811
+ *
812
+ * @param array $page
813
+ * @param array $expectedData
814
+ * @return void
815
+ */
816
+ private function assertResponseData (array $ page , array $ expectedData ): void
817
+ {
818
+ $ this ->assertNotNull ($ page ['id ' ]);
819
+ $ actualData = array_intersect_key ($ page , $ expectedData );
820
+ $ this ->assertEquals ($ expectedData , $ actualData , 'Response data does not match expected. ' );
821
+ }
822
+
823
+ /**
824
+ * Get page grid data of cms ui dataprovider filtering by store code
825
+ *
826
+ * @param string $requestStore
827
+ * @return array
828
+ */
829
+ private function getPageGridDataByStoreCode (string $ requestStore ): array
830
+ {
831
+ if ($ requestStore !== 'all ' ) {
832
+ $ store = $ this ->storeManager ->getStore ($ requestStore );
833
+ $ this ->cmsUiDataProvider ->addFilter (
834
+ $ this ->filterBuilder ->setField ('store_id ' )->setValue ($ store ->getId ())->create ()
835
+ );
836
+ }
837
+
838
+ return $ this ->cmsUiDataProvider ->getData ();
839
+ }
840
+
841
+ /**
842
+ * Get page by identifier without throw exception
843
+ *
844
+ * @param string $identifier
845
+ * @param int $storeId
846
+ * @return PageInterface|null
847
+ */
848
+ private function getPageByIdentifier (string $ identifier , int $ storeId ): ?PageInterface
849
+ {
850
+ $ page = null ;
851
+ try {
852
+ $ page = $ this ->getPageByIdentifier ->execute ($ identifier , $ storeId );
853
+ } catch (NoSuchEntityException $ exception ) {
854
+ }
855
+
856
+ return $ page ;
857
+ }
823
858
}
0 commit comments