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

UserContentContext.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishRestBundle\Features\Context;
8
9 use Behat\Behat\Context\Context;
10 use Behat\Behat\Context\SnippetAcceptingContext;
11 use Behat\Behat\Hook\Scope\BeforeScenarioScope;
12 use Behat\MinkExtension\Context\MinkAwareContext;
13 use Behat\MinkExtension\Context\RawMinkContext;
14 use eZ\Publish\API\Repository\ContentService;
15 use eZ\Publish\API\Repository\ContentTypeService;
16 use eZ\Publish\API\Repository\UserService;
17 use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
18 use eZ\Publish\API\Repository\Values\Content\VersionInfo;
19 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
20 use eZ\Publish\Core\FieldType\User\Value as UserValue;
21 use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
22 use eZ\Publish\Core\REST\Client\Values\Content\Content;
23 use eZ\Publish\Core\REST\Client\Values\Content\VersionUpdate;
24 use eZ\Publish\Core\REST\Server\Values\RestContentCreateStruct;
25 use eZ\Publish\Core\REST\Server\Values\Version;
26 use PHPUnit\Framework\Assert as Assertion;
27
28 class UserContentContext extends RawMinkContext implements Context, SnippetAcceptingContext, MinkAwareContext
29 {
31 private $restContext;
32
34 private $contentService;
35
37 private $contentTypeService;
38
40 private $currentContent;
41
43 private $userService;
44
46 private $currentDraft;
47
53 private $newEmailValue;
54
55 public function __construct(ContentService $contentService, ContentTypeService $contentTypeService, UserService $userService)
56 {
57 $this->contentService = $contentService;
58 $this->contentTypeService = $contentTypeService;
59 $this->userService = $userService;
60 }
61
63 public function gatherContexts(BeforeScenarioScope $scope)
64 {
65 $environment = $scope->getEnvironment();
66 $this->restContext = $environment->getContext('eZ\Bundle\EzPublishRestBundle\Features\Context\RestContext');
67 }
68
72 public function iSetTheContentToUserContentCreateStruct()
73 {
74 $contentCreateStruct = $this->contentService->newContentCreateStruct(
75 $this->contentTypeService->loadContentTypeByIdentifier('user'),
76 'eng-GB'
77 );
78
79 $userFieldValue = new UserValue([
80 'login' => 'user_content_' . time(),
81 'email' => 'user_content_' . time() . '@example.com',
82 'passwordHash' => 'not_a_hash',
83 ]);
84
85 $contentCreateStruct->setField('first_name', new TextLineValue('User Content'));
86 $contentCreateStruct->setField('last_name', new TextLineValue('@' . microtime(true)));
87 $contentCreateStruct->setField('user_account', $userFieldValue);
88
89 $restStruct = new RestContentCreateStruct(
90 $contentCreateStruct,
91 new LocationCreateStruct(['parentLocationId' => 12])
92 );
93 $this->restContext->requestObject = $restStruct;
94 }
95
99 public function itContainsAContentOfContentType($contentTypeIdentifier)
100 {
101 $object = $this->restContext->getResponseObject();
102 Assertion::assertInstanceOf('eZ\Publish\Core\REST\Client\Values\Content\Content', $object);
103 Assertion::assertEquals(
104 $contentTypeIdentifier,
105 $this->contentTypeService->loadContentType($object->contentInfo->contentTypeId)->identifier
106 );
107
108 $this->currentContent = $object;
109 }
110
114 public function itContainsAVersionOfContentType($contentTypeIdentifier)
115 {
116 $object = $this->restContext->getResponseObject();
117 Assertion::assertInstanceOf('eZ\Publish\Core\REST\Server\Values\Version', $object);
118 Assertion::assertEquals(
119 $contentTypeIdentifier,
120 $this->contentTypeService->loadContentType($object->content->contentInfo->contentTypeId)->identifier
121 );
122
123 $this->currentContent = $this->contentService->loadContentByVersionInfo(
124 $object->content->versionInfo
125 );
126 }
127
131 public function aUserWithTheSameIdExists()
132 {
133 Assertion::assertInstanceOf(
134 'eZ\Publish\API\Repository\Values\User\User',
135 $this->userService->loadUser($this->currentContent->id)
136 );
137 }
138
142 public function iSendAPublishRequestForThisContent()
143 {
144 Assertion::assertInstanceOf('\eZ\Publish\API\Repository\Values\Content\Content', $this->currentContent);
145
146 $href = sprintf('/content/objects/%d/versions/1', $this->currentContent->id);
147 $this->restContext->createAndSendRequest('publish', $href);
148 }
149
153 public function thereIsAUserContent()
154 {
155 $login = 'user_content_' . microtime(true);
156 $email = $login . '@example.com';
157
158 $struct = $this->userService->newUserCreateStruct($login, $email, 'PassWord42', 'eng-GB');
159 $struct->setField('first_name', 'John');
160 $struct->setField('last_name', 'Doe');
161
162 $parentGroup = $this->userService->loadUserGroup(11);
163 $user = $this->userService->createUser($struct, [$parentGroup]);
164
165 $this->currentContent = $this->contentService->loadContentByContentInfo($user->contentInfo);
166 }
167
171 public function iCreateDeleteRequestForThisContent()
172 {
173 $this->restContext->createRequest('delete', '/content/objects/' . $this->currentContent->id);
174 }
175
179 public function theUserThisContentReferredToIsDeleted()
180 {
181 // delete the user over HTTP as the user service will have inmemory cache
182 $this->restContext->createAndSendRequest('get', '/user/users/' . $this->currentContent->id);
183 Assertion::assertInstanceOf(
184 'eZ\Publish\Core\REST\Common\Exceptions\NotFoundException',
185 $object = $this->restContext->getResponseObject(),
186 'The user the content referred to exists'
187 );
188 }
189
193 public function theContentHasTheStatus($statusString)
194 {
195 $statusCodeMap = [
196 'published' => VersionInfo::STATUS_PUBLISHED,
197 'draft' => VersionInfo::STATUS_DRAFT,
198 'archived' => VersionInfo::STATUS_DRAFT,
199 ];
200
201 if (!isset($statusCodeMap[$statusString])) {
202 throw new InvalidArgumentException('status string', "Unknown status string $statusString");
203 }
204
205 Assertion::assertEquals(
206 $statusCodeMap[$statusString],
207 $this->currentContent->versionInfo->status
208 );
209 }
210
214 public function iCreateADraftOfThisContent()
215 {
216 $this->restContext->createAndSendRequest('copy', '/content/objects/' . $this->currentContent->id . '/currentversion');
217 $this->currentDraft = $this->restContext->getResponseObject();
218 }
219
223 public function iCreateAnEditRequestForThisDraft()
224 {
225 $url = sprintf(
226 '/content/objects/%d/versions/%d',
227 $this->currentDraft->content->id,
228 $this->currentDraft->content->versionInfo->versionNo
229 );
230 $this->restContext->createRequest('patch', $url);
231
232 $this->restContext->requestObject = new VersionUpdate([
233 'contentUpdateStruct' => $this->contentService->newContentUpdateStruct(),
234 'contentType' => $this->currentDraft->contentType,
235 ]);
236 $this->restContext->setHeaderWithObject('content-type', 'VersionUpdate');
237 }
238
242 public function iSetTheEmailFieldToNewValue()
243 {
244 $this->newEmailValue = 'user_content_' . microtime(true) . '@example.com';
245 $contentUpdateStruct = $this->restContext->requestObject->contentUpdateStruct;
246 $contentUpdateStruct->setField('user_account', new UserValue(['email' => $this->newEmailValue]));
247 }
248
252 public function theUserSEmailWasUpdatedToTheNewValue()
253 {
254 $field = $this->currentContent->getField('user_account');
255 Assertion::assertInstanceOf('eZ\Publish\Core\FieldType\User\Value', $field->value);
256 Assertion::assertEquals($this->newEmailValue, $field->value->email);
257 }
258 }
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext
Definition: UserContentContext.php:29
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\itContainsAVersionOfContentType
itContainsAVersionOfContentType($contentTypeIdentifier)
@Given /^it contains a Version of ContentType "([^"]*)"$/
Definition: UserContentContext.php:114
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\itContainsAContentOfContentType
itContainsAContentOfContentType($contentTypeIdentifier)
@Given /^it contains a Content of ContentType "([^"]*)"$/
Definition: UserContentContext.php:99
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$currentDraft
$currentDraft
Definition: UserContentContext.php:46
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$userService
$userService
Definition: UserContentContext.php:43
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\theContentHasTheStatus
theContentHasTheStatus($statusString)
@Given /^the Content has the "([^"]*)" status$/
Definition: UserContentContext.php:193
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\theUserSEmailWasUpdatedToTheNewValue
theUserSEmailWasUpdatedToTheNewValue()
@Given /^the User's email was updated to the new value$/
Definition: UserContentContext.php:252
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iSendAPublishRequestForThisContent
iSendAPublishRequestForThisContent()
@When /^I send a publish request for this content$/
Definition: UserContentContext.php:142
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iSetTheEmailFieldToNewValue
iSetTheEmailFieldToNewValue()
@Given /^I set the email field to a new value$/
Definition: UserContentContext.php:242
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iCreateDeleteRequestForThisContent
iCreateDeleteRequestForThisContent()
@When /^I create a delete request for this content$/
Definition: UserContentContext.php:171
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$currentContent
$currentContent
Definition: UserContentContext.php:40
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$newEmailValue
$newEmailValue
Definition: UserContentContext.php:53
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iSetTheContentToUserContentCreateStruct
iSetTheContentToUserContentCreateStruct()
@Given /^I set the Content to a User RestContentCreateStruct$/
Definition: UserContentContext.php:72
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\thereIsAUserContent
thereIsAUserContent()
@Given /^there is a User Content$/
Definition: UserContentContext.php:153
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$contentTypeService
$contentTypeService
Definition: UserContentContext.php:37
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$restContext
$restContext
Definition: UserContentContext.php:31
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\aUserWithTheSameIdExists
aUserWithTheSameIdExists()
@Given /^a User with the same id exists$/
Definition: UserContentContext.php:131
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\gatherContexts
gatherContexts(BeforeScenarioScope $scope)
@BeforeScenario
Definition: UserContentContext.php:63
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\__construct
__construct(ContentService $contentService, ContentTypeService $contentTypeService, UserService $userService)
Definition: UserContentContext.php:55
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iCreateADraftOfThisContent
iCreateADraftOfThisContent()
@When /^I create a draft of this content$/
Definition: UserContentContext.php:214
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\iCreateAnEditRequestForThisDraft
iCreateAnEditRequestForThisDraft()
@When /^I create an edit request for this draft$/
Definition: UserContentContext.php:223
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\$contentService
$contentService
Definition: UserContentContext.php:34
eZ\Bundle\EzPublishRestBundle\Features\Context\UserContentContext\theUserThisContentReferredToIsDeleted
theUserThisContentReferredToIsDeleted()
@Then /^the User this Content referred to is deleted$/
Definition: UserContentContext.php:179
eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
This class is used to create a new Location for a content object.
Definition: API/Repository/Values/Content/LocationCreateStruct.php:15
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value
Struct that stores extra value information for a Criterion object.
Definition: API/Repository/Values/Content/Query/Criterion/Value.php:13
eZ\Publish\API\Repository\Values\Content\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\API\Repository\Values\Content\VersionInfo\STATUS_DRAFT
const STATUS_DRAFT
Definition: API/Repository/Values/Content/VersionInfo.php:28
eZ\Publish\API\Repository\Values\Content\VersionInfo\STATUS_PUBLISHED
const STATUS_PUBLISHED
Definition: API/Repository/Values/Content/VersionInfo.php:29
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\FieldType\User\Value
Value for User field type.
Definition: Core/FieldType/User/Value.php:15
eZ\Publish\Core\REST\Client\Values\Content\Content
Definition: Publish/Core/REST/Client/Values/Content/Content.php:26
eZ\Publish\Core\REST\Client\Values\Content\VersionUpdate
Definition: Client/Values/Content/VersionUpdate.php:21
eZ\Publish\Core\REST\Server\Values\RestContentCreateStruct
RestContentCreateStruct view model.
Definition: Server/Values/RestContentCreateStruct.php:17
eZ\Publish\Core\REST\Server\Values\Version
Version view model.
Definition: Core/REST/Server/Values/Version.php:17
eZ\Publish\API\Repository\ContentService
Definition: API/Repository/ContentService.php:27
eZ\Publish\API\Repository\ContentTypeService
Definition: API/Repository/ContentTypeService.php:25
eZ\Publish\API\Repository\UserService
Definition: API/Repository/UserService.php:26
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.