-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Get existing page data only on edit page #39746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mohamed-Asar
wants to merge
8
commits into
magento:2.4-develop
Choose a base branch
from
Mohamed-Asar:issue/39743-cms-page-error-log-fix
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8a2af09
Get existing page data only on edit page
Mohamed-Asar f1fb5e2
Merge branch '2.4-develop' into issue/39743-cms-page-error-log-fix
engcom-Hotel bd6c526
Merge branch '2.4-develop' of https://github.com/Mohamed-Asar/magento…
Mohamed-Asar f8474ec
Static test fix - reduce coupling in page data provider with service
Mohamed-Asar 2c1b4db
Strict types added
Mohamed-Asar 5aba121
Merge branch '2.4-develop' into issue/39743-cms-page-error-log-fix
engcom-Hotel b3b389e
Unit test added for page service class and code review changes
Mohamed-Asar aaf1c99
Merge branch '2.4-develop' into issue/39743-cms-page-error-log-fix
engcom-Dash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* Copyright 2015 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
namespace Magento\Cms\Model\Page\Service; | ||
|
||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Cms\Model\Page\CustomLayoutManagerInterface; | ||
use Magento\Cms\Model\PageFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
|
||
/** | ||
* Cms Page Service Class | ||
*/ | ||
class PageService | ||
{ | ||
/** | ||
* @param PageRepositoryInterface $pageRepository | ||
* @param PageFactory $pageFactory | ||
* @param CustomLayoutManagerInterface $customLayoutManager | ||
*/ | ||
public function __construct( | ||
private readonly PageRepositoryInterface $pageRepository, | ||
private readonly PageFactory $pageFactory, | ||
private readonly CustomLayoutManagerInterface $customLayoutManager, | ||
) { | ||
} | ||
|
||
/** | ||
* To get the page by its ID. If the page not exists, a new page instance is returned | ||
* | ||
* @param int $id | ||
* @return PageInterface | ||
*/ | ||
public function getPageById(int $id): PageInterface | ||
{ | ||
try { | ||
return $this->pageRepository->getById($id); | ||
} catch (LocalizedException) { | ||
return $this->pageFactory->create(); | ||
} | ||
} | ||
|
||
/** | ||
* To create pagefactory class | ||
* | ||
* @return PageInterface | ||
*/ | ||
public function createPageFactory(): PageInterface | ||
{ | ||
return $this->pageFactory->create(); | ||
} | ||
|
||
/** | ||
* Fetches the available custom layouts for a given page. | ||
* | ||
* @param PageInterface $page | ||
* @return array | ||
*/ | ||
public function fetchAvailableCustomLayouts(PageInterface $page): array | ||
{ | ||
return $this->customLayoutManager->fetchAvailableFiles($page); | ||
} | ||
} |
182 changes: 182 additions & 0 deletions
182
app/code/Magento/Cms/Test/Unit/Model/Page/Service/PageServiceTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
/** | ||
* Copyright 2025 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Cms\Test\Unit\Model\Page\Service; | ||
|
||
use Magento\Cms\Api\Data\PageInterface; | ||
use Magento\Cms\Api\PageRepositoryInterface; | ||
use Magento\Cms\Model\Page\CustomLayoutManagerInterface; | ||
use Magento\Cms\Model\Page\Service\PageService; | ||
use Magento\Cms\Model\PageFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for PageService | ||
*/ | ||
class PageServiceTest extends TestCase | ||
{ | ||
/** | ||
* @var PageRepositoryInterface|MockObject | ||
*/ | ||
private $pageRepositoryMock; | ||
|
||
/** | ||
* @var PageFactory|MockObject | ||
*/ | ||
private $pageFactoryMock; | ||
|
||
/** | ||
* @var CustomLayoutManagerInterface|MockObject | ||
*/ | ||
private $customLayoutManagerMock; | ||
|
||
/** | ||
* @var PageService | ||
*/ | ||
private $pageService; | ||
|
||
/** | ||
* @var PageInterface|MockObject | ||
*/ | ||
private $pageMock; | ||
|
||
/** | ||
* Set up test environment | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->pageRepositoryMock = $this->createMock(PageRepositoryInterface::class); | ||
$this->pageFactoryMock = $this->createMock(PageFactory::class); | ||
$this->customLayoutManagerMock = $this->createMock(CustomLayoutManagerInterface::class); | ||
$this->pageMock = $this->createMock(PageInterface::class); | ||
|
||
$this->pageService = new PageService( | ||
$this->pageRepositoryMock, | ||
$this->pageFactoryMock, | ||
$this->customLayoutManagerMock | ||
); | ||
} | ||
|
||
/** | ||
* Test getPageById returns existing page when page exists | ||
*/ | ||
public function testGetPageByIdReturnsExistingPage(): void | ||
{ | ||
$pageId = 1; | ||
|
||
$this->pageRepositoryMock->expects($this->once()) | ||
->method('getById') | ||
->with($pageId) | ||
->willReturn($this->pageMock); | ||
|
||
$this->pageFactoryMock->expects($this->never()) | ||
->method('create'); | ||
|
||
$result = $this->pageService->getPageById($pageId); | ||
|
||
$this->assertSame($this->pageMock, $result); | ||
} | ||
|
||
/** | ||
* Test getPageById returns new page instance when page doesn't exist | ||
*/ | ||
public function testGetPageByIdReturnsNewPageWhenNotExists(): void | ||
{ | ||
$pageId = 999; | ||
$newPageMock = $this->createMock(PageInterface::class); | ||
|
||
$this->pageRepositoryMock->expects($this->once()) | ||
->method('getById') | ||
->with($pageId) | ||
->willThrowException(new NoSuchEntityException(__('Page not found'))); | ||
|
||
$this->pageFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($newPageMock); | ||
|
||
$result = $this->pageService->getPageById($pageId); | ||
|
||
$this->assertSame($newPageMock, $result); | ||
} | ||
|
||
/** | ||
* Test getPageById handles LocalizedException and returns new page | ||
*/ | ||
public function testGetPageByIdHandlesLocalizedException(): void | ||
{ | ||
$pageId = 1; | ||
$newPageMock = $this->createMock(PageInterface::class); | ||
|
||
$this->pageRepositoryMock->expects($this->once()) | ||
->method('getById') | ||
->with($pageId) | ||
->willThrowException(new LocalizedException(__('Some localized error'))); | ||
|
||
$this->pageFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($newPageMock); | ||
|
||
$result = $this->pageService->getPageById($pageId); | ||
|
||
$this->assertSame($newPageMock, $result); | ||
} | ||
|
||
/** | ||
* Test createPageFactory returns new page instance | ||
*/ | ||
public function testCreatePageFactory(): void | ||
{ | ||
$newPageMock = $this->createMock(PageInterface::class); | ||
|
||
$this->pageFactoryMock->expects($this->once()) | ||
->method('create') | ||
->willReturn($newPageMock); | ||
|
||
$result = $this->pageService->createPageFactory(); | ||
|
||
$this->assertSame($newPageMock, $result); | ||
} | ||
|
||
/** | ||
* Test fetchAvailableCustomLayouts returns available layouts | ||
*/ | ||
public function testFetchAvailableCustomLayouts(): void | ||
{ | ||
$expectedLayouts = [ | ||
'layout1.xml', | ||
'layout2.xml', | ||
'custom_layout.xml' | ||
]; | ||
|
||
$this->customLayoutManagerMock->expects($this->once()) | ||
->method('fetchAvailableFiles') | ||
->with($this->pageMock) | ||
->willReturn($expectedLayouts); | ||
|
||
$result = $this->pageService->fetchAvailableCustomLayouts($this->pageMock); | ||
|
||
$this->assertSame($expectedLayouts, $result); | ||
} | ||
|
||
/** | ||
* Test fetchAvailableCustomLayouts returns empty array when no layouts available | ||
*/ | ||
public function testFetchAvailableCustomLayoutsReturnsEmptyArray(): void | ||
{ | ||
$this->customLayoutManagerMock->expects($this->once()) | ||
->method('fetchAvailableFiles') | ||
->with($this->pageMock) | ||
->willReturn([]); | ||
|
||
$result = $this->pageService->fetchAvailableCustomLayouts($this->pageMock); | ||
|
||
$this->assertSame([], $result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@engcom-Hotel i have added unit test case, please review