Skip to content

Commit 90b454c

Browse files
committed
[Feature] Add integration tests for PageExporter
Add Menu and Controller route for export page incl. subpages
1 parent 5d51a9c commit 90b454c

File tree

18 files changed

+247
-21
lines changed

18 files changed

+247
-21
lines changed

compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
timeout: 10s
1717

1818
php:
19-
image: pimcore/pimcore:php8.3-latest
19+
image: pimcore/pimcore:php8.3-debug-latest
2020
volumes:
2121
- ./:/var/www/html/
2222
environment:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"phpstan/phpstan-symfony": "^1.3.8",
2727
"phpunit/phpunit": "^9.5",
2828
"pimcore/admin-ui-classic-bundle": "^1.6",
29+
"spatie/phpunit-snapshot-assertions": "^4.2",
2930
"teamneusta/pimcore-testing-framework": "^0.12"
3031
},
3132
"autoload": {

config/pimcore/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ neusta_converter:
3131
path:
3232
source: path
3333
default: '/'
34+
skip_null: true
3435
parentId:
3536
source: parentId
3637
default: 0

config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ services:
4141
arguments:
4242
$propertyKey: 'language'
4343
$targetProperty: 'language'
44+
$skipNull: true
4445

4546
neusta_pimcore_import_export.page.property.navigation_title.populator:
4647
class: Neusta\Pimcore\ImportExportBundle\PimcoreConverter\Populator\PropertyBasedMappingPopulator
4748
arguments:
4849
$propertyKey: 'navigation_title'
4950
$targetProperty: 'navigation_title'
51+
$skipNull: true
5052

5153
neusta_pimcore_import_export.page.property.navigation_name.populator:
5254
class: Neusta\Pimcore\ImportExportBundle\PimcoreConverter\Populator\PropertyBasedMappingPopulator
5355
arguments:
5456
$propertyKey: 'navigation_name'
5557
$targetProperty: 'navigation_name'
58+
$skipNull: true
5659

5760
Neusta\Pimcore\ImportExportBundle\Documents\Export\PageExporter:
5861
arguments:

public/js/exportPage.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ neusta_pimcore_import_export.plugin.page.export = Class.create({
88
onPrepareDocumentTreeContextMenu: function (e) {
99
let menu = e.detail.menu;
1010
let document = e.detail.document;
11+
1112
// Export page into yaml file
1213
menu.add("-");
1314
menu.add(new Ext.menu.Item({
@@ -17,6 +18,15 @@ neusta_pimcore_import_export.plugin.page.export = Class.create({
1718
pimcore.helpers.download(Routing.generate('neusta_pimcore_import_export_page_export', {page_id: document.data.id}));
1819
}
1920
}));
21+
22+
// Export page and children into yaml file
23+
menu.add(new Ext.menu.Item({
24+
text: t('neusta_pimcore_import_export_export_with_children_menu_label'),
25+
iconCls: "pimcore_icon_export",
26+
handler: function () {
27+
pimcore.helpers.download(Routing.generate('neusta_pimcore_import_export_page_export_with_children', {page_id: document.data.id}));
28+
}
29+
}));
2030
},
2131

2232
});

src/Controller/Admin/PageExportController.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,40 @@ public function exportPage(Request $request): Response
3737
}
3838

3939
try {
40-
$yaml = $this->pageExporter->toYaml([$page]);
40+
$yaml = $this->pageExporter->exportToYaml([$page]);
41+
} catch (\Exception $e) {
42+
return new JsonResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
43+
}
44+
45+
$response = new Response($yaml);
46+
$response->headers->set('Content-type', 'application/yaml');
47+
$response->headers->set(
48+
'Content-Disposition',
49+
HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $this->createFilename($page)),
50+
);
51+
52+
return $response;
53+
}
54+
55+
#[Route(
56+
'/admin/neusta/import-export/page/export/with-children',
57+
name: 'neusta_pimcore_import_export_page_export_with_children',
58+
methods: ['GET']
59+
)]
60+
public function exportPageWithChildren(Request $request): Response
61+
{
62+
$pageId = $request->query->getInt('page_id');
63+
$page = $this->pageRepository->getById($pageId);
64+
65+
if (!$page instanceof Page) {
66+
return new JsonResponse(
67+
\sprintf('Page with id "%s" was not found', $pageId),
68+
Response::HTTP_NOT_FOUND,
69+
);
70+
}
71+
72+
try {
73+
$yaml = $this->pageExporter->exportToYaml($this->pageRepository->findAllPagesWithSubPages($page));
4174
} catch (\Exception $e) {
4275
return new JsonResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
4376
}

src/Controller/Admin/PageImportController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ final class PageImportController
1919
public const SUCCESS_WITHOUT_REPLACEMENT = 3;
2020
public const SUCCESS_NEW_DOCUMENT = 4;
2121

22-
/** string[] */
22+
/**
23+
* @var string[] Map of error codes to messages
24+
*/
2325
private array $messagesMap;
2426

2527
public function __construct(
@@ -84,7 +86,7 @@ protected function replaceIfExists(Page $page, bool $overwrite): int
8486

8587
private function appendMessage(int|string $index, int $resultCode, string $resultMessage): string
8688
{
87-
$message = \sprintf('%d. %s', $index + 1, $this->messagesMap[$resultCode]);
89+
$message = \sprintf('%d. %s', (int) $index + 1, $this->messagesMap[$resultCode]);
8890
$resultMessage .= $message . \PHP_EOL;
8991

9092
return $resultMessage;

src/Documents/Base/AbstractPageFixture.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ private function internalCreate(): void
4848
$this->fullqualifiedYamlFilename,
4949
$this->params,
5050
);
51-
$page = $this->pageImporter->parseYaml($yamlContent, false);
52-
$this->replaceIfExists($page);
51+
$pages = $this->pageImporter->parseYaml($yamlContent, false);
52+
foreach ($pages as $page) {
53+
$this->replaceIfExists($page);
54+
}
5355
}
5456
}

src/Documents/Export/PageExporter.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class PageExporter
1313
{
14-
private const YAML_DUMP_FLAGS =
14+
public const YAML_DUMP_FLAGS =
1515
Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE |
1616
Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK |
1717
Yaml::DUMP_NULL_AS_TILDE;
@@ -34,19 +34,35 @@ public function __construct(
3434
* key: 'page_key_2'
3535
* ...
3636
*
37-
* @param Page|iterable<Page> $pages
38-
*
3937
* @throws ConverterException
4038
*
41-
* @deprecated parameter type Page will not be allowed in further versions
39+
* @deprecated use PageExporter#exportToYaml([$page]) in further versions
4240
*/
43-
public function toYaml(Page|iterable $pages): string
41+
public function toYaml(Page $page): string
4442
{
45-
// @deprecated - should be removed after changing the method signature
46-
if ($pages instanceof Page) {
47-
$pages = [$pages];
43+
$pages = [];
44+
if ($page instanceof Page) {
45+
$pages = [$page];
4846
}
4947

48+
return $this->exportToYaml($pages);
49+
}
50+
51+
/**
52+
* Exports one or more pages as YAML with the following structure:
53+
* pages:
54+
* - page:
55+
* key: 'page_key_1'
56+
* - page:
57+
* key: 'page_key_2'
58+
* ...
59+
*
60+
* @param iterable<Page> $pages
61+
*
62+
* @throws ConverterException
63+
*/
64+
public function exportToYaml(iterable $pages): string
65+
{
5066
$yamlExportPages = [];
5167
foreach ($pages as $page) {
5268
$yamlExportPages[] = [YamlExportPage::PAGE => $this->pageToYamlConverter->convert($page)];
@@ -57,7 +73,7 @@ public function toYaml(Page|iterable $pages): string
5773
'yaml',
5874
[
5975
'yaml_inline' => 4,
60-
'yaml_indent' => 0,
76+
'yaml_indent' => 2,
6177
'yaml_flags' => self::YAML_DUMP_FLAGS,
6278
]
6379
);

src/Documents/Export/YamlExportPage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class YamlExportPage
1313
public bool $published = false;
1414
public string $path = '';
1515
public string $language = '';
16-
public string $navigation_name = '';
16+
public ?string $navigation_name = null;
1717
public ?string $navigation_title = null;
1818
public string $key = '';
19-
public string $title = '';
20-
public string $controller = '';
19+
public ?string $title = null;
20+
public ?string $controller = null;
2121
/** @var array<YamlExportEditable> */
2222
public array $editables = [];
2323

0 commit comments

Comments
 (0)