Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ContentContext.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9 use Behat\Behat\Context\Context;
10 use Behat\Behat\Context\SnippetAcceptingContext;
11 use eZ\Publish\API\Repository\Repository;
12 use eZ\Publish\API\Repository\Values\Content\Content;
13 use RuntimeException;
14
15 class ContentContext implements Context, SnippetAcceptingContext
16 {
18 private $currentContent;
19
21 private $currentDraft;
22
24 private $repository;
25
26 public function __construct(Repository $repository)
27 {
28 $this->repository = $repository;
29 }
30
34 public function iCreateAnFolderDraft()
35 {
36 $this->currentDraft = $this->createDraft(
37 'folder',
38 [
39 'name' => 'Preview draft ' . date('c'),
40 'short_description' => '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"><para>This is a paragraph.</para></section>',
41 ]
42 );
43 }
44
48 public function iCreateADraftOfAnExistingContentItem()
49 {
50 $this->currentContent = $this->createContentItem(
51 'folder',
52 ['name' => 'BDD preview test']
53 );
54
55 $this->currentDraft = $this->createDraftForContent($this->currentContent);
56 }
57
67 public function createContentItem($contentTypeIdentifier, array $fields)
68 {
69 $draft = $this->createDraft($contentTypeIdentifier, $fields);
70
71 $this->currentContent = $this->repository->sudo(
72 function () use ($draft) {
73 return $this->repository->getContentService()->publishVersion($draft->versionInfo);
74 }
75 );
76
77 $this->currentDraft = null;
78
79 return $this->currentContent;
80 }
81
82 public function createDraftForContent(Content $content)
83 {
84 $this->currentDraft = $this->repository->sudo(
85 function () use ($content) {
86 return $this->repository->getContentService()->createContentDraft($content->contentInfo);
87 }
88 );
89
90 return $this->currentDraft;
91 }
92
93 public function getCurrentDraft()
94 {
95 if ($this->currentDraft === null) {
96 throw new RuntimeException('No current draft has been set');
97 }
98
99 return $this->currentDraft;
100 }
101
102 public function updateDraft($fields)
103 {
104 $contentService = $this->repository->getContentService();
105
106 $updateStruct = $contentService->newContentUpdateStruct();
107 foreach ($fields as $fieldDefIdentifier => $fieldValueUpdate) {
108 $updateStruct->setField($fieldDefIdentifier, $fieldValueUpdate);
109 }
110
111 $updatedDraft = $this->repository->sudo(function () use ($updateStruct) {
112 return $this->repository->getContentService()->updateContent(
113 $this->currentDraft->versionInfo,
114 $updateStruct
115 );
116 });
117
118 return $this->currentDraft = $updatedDraft;
119 }
120
130 public function createDraft($contentTypeIdentifier, array $fields)
131 {
132 $contentService = $this->repository->getContentService();
133
134 $createStruct = $contentService->newContentCreateStruct(
135 $this->repository->getContentTypeService()->loadContentTypeByIdentifier($contentTypeIdentifier),
136 'eng-GB'
137 );
138
139 foreach ($fields as $fieldDefIdentifier => $fieldValue) {
140 $createStruct->setField($fieldDefIdentifier, $fieldValue);
141 }
142
143 $locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct(2);
144
145 $this->currentDraft = $this->repository->sudo(
146 function () use ($createStruct, $locationCreateStruct) {
147 return $this->repository->getContentService()->createContent(
148 $createStruct,
149 [$locationCreateStruct]
150 );
151 }
152 );
153
154 return $this->currentDraft;
155 }
156 }
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext
Definition: ContentContext.php:16
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\iCreateAnFolderDraft
iCreateAnFolderDraft()
@Given /^I create an folder draft$/
Definition: ContentContext.php:34
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\$repository
$repository
Definition: ContentContext.php:24
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\createContentItem
createContentItem($contentTypeIdentifier, array $fields)
Uses a content type identifier + a hash of fields values to create and publish a content item below t...
Definition: ContentContext.php:67
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\createDraft
createDraft($contentTypeIdentifier, array $fields)
Uses a content type identifier + a hash of fields values to create and publish a draft below the root...
Definition: ContentContext.php:130
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\$currentContent
$currentContent
Definition: ContentContext.php:18
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\createDraftForContent
createDraftForContent(Content $content)
Definition: ContentContext.php:82
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\iCreateADraftOfAnExistingContentItem
iCreateADraftOfAnExistingContentItem()
@Given /^I create a draft of an existing content item$/
Definition: ContentContext.php:48
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\updateDraft
updateDraft($fields)
Definition: ContentContext.php:102
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\__construct
__construct(Repository $repository)
Definition: ContentContext.php:26
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\$currentDraft
$currentDraft
Definition: ContentContext.php:21
eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext\getCurrentDraft
getCurrentDraft()
Definition: ContentContext.php:93
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Bundle\EzPublishCoreBundle\Features\Context
Definition: BasicContentContext.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.