|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context; |
|
6
|
|
|
|
|
7
|
|
|
use Behat\Behat\Context\Context; |
|
8
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
|
9
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
|
|
13
|
|
|
class ContentContext implements Context, SnippetAcceptingContext |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Content */ |
|
16
|
|
|
private $currentContent; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Content */ |
|
19
|
|
|
private $currentDraft; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \eZ\Publish\API\Repository\Repository */ |
|
22
|
|
|
private $repository; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Repository $repository) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->repository = $repository; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @Given /^I create an article draft$/ |
|
31
|
|
|
*/ |
|
32
|
|
|
public function iCreateAnArticleDraft() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->currentDraft = $this->createDraft( |
|
35
|
|
|
'article', |
|
36
|
|
|
[ |
|
37
|
|
|
'title' => 'Preview draft ' . date('c'), |
|
38
|
|
|
'intro' => '<?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>', |
|
39
|
|
|
] |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @Given /^I create a draft of an existing content item$/ |
|
45
|
|
|
*/ |
|
46
|
|
|
public function iCreateADraftOfAnExistingContentItem() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->currentContent = $this->createContentItem( |
|
49
|
|
|
'folder', |
|
50
|
|
|
['name' => 'BDD preview test'] |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$this->currentDraft = $this->createDraftForContent($this->currentContent); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Uses a content type identifier + a hash of fields values |
|
58
|
|
|
* to create and publish a content item below the root location. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $contentTypeIdentifier |
|
61
|
|
|
* @param array $fields Hash of field def identifier => field value |
|
62
|
|
|
* |
|
63
|
|
|
* @return Content the created content item. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function createContentItem($contentTypeIdentifier, array $fields) |
|
66
|
|
|
{ |
|
67
|
|
|
$draft = $this->createDraft($contentTypeIdentifier, $fields); |
|
68
|
|
|
|
|
69
|
|
|
$this->currentContent = $this->repository->sudo( |
|
|
|
|
|
|
70
|
|
|
function () use ($draft) { |
|
71
|
|
|
return $this->repository->getContentService()->publishVersion($draft->versionInfo); |
|
72
|
|
|
} |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->currentDraft = null; |
|
76
|
|
|
|
|
77
|
|
|
return $this->currentContent; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function createDraftForContent(Content $content) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->currentDraft = $this->repository->sudo( |
|
|
|
|
|
|
83
|
|
|
function () use ($content) { |
|
84
|
|
|
return $this->repository->getContentService()->createContentDraft($content->contentInfo); |
|
85
|
|
|
} |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return $this->currentDraft; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getCurrentDraft() |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->currentDraft === null) { |
|
94
|
|
|
throw new RuntimeException('No current draft has been set'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->currentDraft; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function updateDraft($fields) |
|
101
|
|
|
{ |
|
102
|
|
|
$contentService = $this->repository->getContentService(); |
|
103
|
|
|
|
|
104
|
|
|
$updateStruct = $contentService->newContentUpdateStruct(); |
|
105
|
|
|
foreach ($fields as $fieldDefIdentifier => $fieldValueUpdate) { |
|
106
|
|
|
$updateStruct->setField($fieldDefIdentifier, $fieldValueUpdate); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$updatedDraft = $this->repository->sudo(function () use ($updateStruct) { |
|
|
|
|
|
|
110
|
|
|
return $this->repository->getContentService()->updateContent( |
|
111
|
|
|
$this->currentDraft->versionInfo, |
|
112
|
|
|
$updateStruct |
|
113
|
|
|
); |
|
114
|
|
|
}); |
|
115
|
|
|
|
|
116
|
|
|
return $this->currentDraft = $updatedDraft; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Uses a content type identifier + a hash of fields values |
|
121
|
|
|
* to create and publish a draft below the root location. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $contentTypeIdentifier |
|
124
|
|
|
* @param array $fields Hash of field def identifier => field value |
|
125
|
|
|
* |
|
126
|
|
|
* @return Content the created draft. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function createDraft($contentTypeIdentifier, array $fields) |
|
129
|
|
|
{ |
|
130
|
|
|
$contentService = $this->repository->getContentService(); |
|
131
|
|
|
|
|
132
|
|
|
$createStruct = $contentService->newContentCreateStruct( |
|
133
|
|
|
$this->repository->getContentTypeService()->loadContentTypeByIdentifier($contentTypeIdentifier), |
|
134
|
|
|
'eng-GB' |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
foreach ($fields as $fieldDefIdentifier => $fieldValue) { |
|
138
|
|
|
$createStruct->setField($fieldDefIdentifier, $fieldValue); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct(2); |
|
142
|
|
|
|
|
143
|
|
|
$this->currentDraft = $this->repository->sudo( |
|
|
|
|
|
|
144
|
|
|
function () use ($createStruct, $locationCreateStruct) { |
|
145
|
|
|
return $this->repository->getContentService()->createContent( |
|
146
|
|
|
$createStruct, |
|
147
|
|
|
[$locationCreateStruct] |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
return $this->currentDraft; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: