Skip to content

Commit 8dc9c1a

Browse files
committed
MC-37542: Create automated test for "[API] Create CMS page using API service"
1 parent b79db54 commit 8dc9c1a

File tree

1 file changed

+97
-62
lines changed

1 file changed

+97
-62
lines changed

dev/tests/api-functional/testsuite/Magento/Cms/Api/PageRepositoryTest.php

Lines changed: 97 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Cms\Api;
79

810
use Magento\Authorization\Model\Role;
@@ -191,15 +193,12 @@ public function testGet(): void
191193
*/
192194
public function testGetByStores(string $requestStore): void
193195
{
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);
200199
$expectedData = array_intersect_key(
201200
$this->dataObjectProcessor->buildOutputDataArray($page, PageInterface::class),
202-
$comparedFields
201+
$this->getPageRequestData()['page']
203202
);
204203
$serviceInfo = $this->getServiceInfo(
205204
'GetById',
@@ -212,9 +211,7 @@ public function testGetByStores(string $requestStore): void
212211
}
213212

214213
$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);
218215
}
219216

220217
/**
@@ -255,27 +252,17 @@ public function testCreate(): void
255252
*/
256253
public function testCreateByStores(string $requestStore): void
257254
{
255+
$newStoreId = $this->getStoreIdByRequestStore($requestStore);
258256
$serviceInfo = $this->getServiceInfo('Save', Request::HTTP_METHOD_POST);
259257
$requestData = $this->getPageRequestData();
258+
260259
$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);
276263
$this->assertTrue(
277264
$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)
279266
);
280267
}
281268

@@ -373,10 +360,8 @@ public function testUpdateOneField(): void
373360
*/
374361
public function testUpdateByStores(string $requestStore): void
375362
{
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]);
380365
$serviceInfo = $this->getServiceInfo(
381366
'Save',
382367
Request::HTTP_METHOD_PUT,
@@ -385,22 +370,12 @@ public function testUpdateByStores(string $requestStore): void
385370
$requestData = $this->getPageRequestData();
386371

387372
$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);
401376
$this->assertTrue(
402377
$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)
404379
);
405380
}
406381

@@ -440,10 +415,8 @@ public function testDelete(): void
440415
*/
441416
public function testDeleteByStores(string $requestStore): void
442417
{
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]);
447420
$serviceInfo = $this->getServiceInfo(
448421
'DeleteById',
449422
Request::HTTP_METHOD_DELETE,
@@ -453,17 +426,13 @@ public function testDeleteByStores(string $requestStore): void
453426
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
454427
$requestData[PageInterface::PAGE_ID] = $page->getId();
455428
}
429+
456430
$pageResponse = $this->_webApiCall($serviceInfo, $requestData, null, $requestStore);
457431
$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);
464433
$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)
467436
);
468437
}
469438

@@ -561,12 +530,12 @@ public function byStoresProvider(): array
561530
'default_store' => [
562531
'request_store' => 'default',
563532
],
564-
/*'second_store' => [
533+
'second_store' => [
565534
'request_store' => 'fixture_second_store',
566535
],
567536
'all' => [
568537
'request_store' => 'all',
569-
],*/
538+
],
570539
];
571540
}
572541

@@ -606,9 +575,9 @@ private function prepareCmsPages(): array
606575
/**
607576
* Create page with hard-coded identifier to test with create-delete-create flow.
608577
* @param string $identifier
609-
* @return string
578+
* @return int
610579
*/
611-
private function createPageWithIdentifier($identifier): string
580+
private function createPageWithIdentifier($identifier): int
612581
{
613582
$serviceInfo = $this->getServiceInfo('Save', Request::HTTP_METHOD_POST);
614583
$requestData = [
@@ -792,12 +761,14 @@ private function isPageInArray(array $pageGridData, int $pageId): bool
792761
/**
793762
* Update page with data
794763
*
795-
* @param PageInterface $page
764+
* @param string $pageIdentifier
765+
* @param int $storeId
796766
* @param array $pageData
797767
* @return PageInterface
798768
*/
799-
private function updatePage(PageInterface $page, array $pageData): PageInterface
769+
private function updatePage(string $pageIdentifier, int $storeId, array $pageData): PageInterface
800770
{
771+
$page = $this->getPageByIdentifier->execute($pageIdentifier, $storeId);
801772
$page->addData($pageData);
802773

803774
return $this->pageRepository->save($page);
@@ -820,4 +791,68 @@ private function getPageRequestData(): array
820791
]
821792
];
822793
}
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+
}
823858
}

0 commit comments

Comments
 (0)