1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\BehatExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\ClosuredContextInterface, |
6
|
|
|
Behat\Behat\Context\TranslatedContextInterface, |
7
|
|
|
Behat\Behat\Context\BehatContext, |
8
|
|
|
Behat\Behat\Context\Step, |
9
|
|
|
Behat\Behat\Event\FeatureEvent, |
10
|
|
|
Behat\Behat\Event\ScenarioEvent, |
11
|
|
|
Behat\Behat\Exception\PendingException; |
12
|
|
|
use Behat\Gherkin\Node\PyStringNode, |
13
|
|
|
Behat\Gherkin\Node\TableNode; |
14
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
15
|
|
|
|
16
|
|
|
// PHPUnit |
17
|
|
|
require_once 'PHPUnit/Autoload.php'; |
18
|
|
|
require_once 'PHPUnit/Framework/Assert/Functions.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Context used to define steps related to email sending. |
22
|
|
|
*/ |
23
|
|
|
class EmailContext extends BehatContext |
24
|
|
|
{ |
25
|
|
|
protected $context; |
26
|
|
|
|
27
|
|
|
protected $mailer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Stored to simplify later assertions |
31
|
|
|
*/ |
32
|
|
|
protected $lastMatchedEmail; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes context. |
36
|
|
|
* Every scenario gets it's own context object. |
37
|
|
|
* |
38
|
|
|
* @param array $parameters context parameters (set them up through behat.yml) |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $parameters) |
41
|
|
|
{ |
42
|
|
|
// Initialize your context here |
43
|
|
|
$this->context = $parameters; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get Mink session from MinkContext |
48
|
|
|
*/ |
49
|
|
|
public function getSession($name = null) |
50
|
|
|
{ |
51
|
|
|
return $this->getMainContext()->getSession($name); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @BeforeScenario |
56
|
|
|
*/ |
57
|
|
|
public function before(ScenarioEvent $event) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
// Also set through the 'supportbehat' extension |
60
|
|
|
// to ensure its available both in CLI execution and the tested browser session |
61
|
|
|
$this->mailer = new \SilverStripe\BehatExtension\Utility\TestMailer(); |
62
|
|
|
\Email::set_mailer($this->mailer); |
|
|
|
|
63
|
|
|
\Config::inst()->update("Email","send_all_emails_to", null); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Given /^there should (not |)be an email (to|from) "([^"]*)"$/ |
68
|
|
|
*/ |
69
|
|
|
public function thereIsAnEmailFromTo($negate, $direction, $email) |
70
|
|
|
{ |
71
|
|
|
$to = ($direction == 'to') ? $email : null; |
72
|
|
|
$from = ($direction == 'from') ? $email : null; |
73
|
|
|
$match = $this->mailer->findEmail($to, $from); |
74
|
|
|
if(trim($negate)) { |
75
|
|
|
assertNull($match); |
76
|
|
|
} else { |
77
|
|
|
assertNotNull($match); |
78
|
|
|
} |
79
|
|
|
$this->lastMatchedEmail = $match; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @Given /^there should (not |)be an email (to|from) "([^"]*)" titled "([^"]*)"$/ |
84
|
|
|
*/ |
85
|
|
|
public function thereIsAnEmailFromToTitled($negate, $direction, $email, $subject) |
86
|
|
|
{ |
87
|
|
|
$to = ($direction == 'to') ? $email : null; |
88
|
|
|
$from = ($direction == 'from') ? $email : null; |
89
|
|
|
$match = $this->mailer->findEmail($to, $from, $subject); |
90
|
|
|
$allMails = $this->mailer->findEmails($to, $from); |
91
|
|
|
$allTitles = $allMails ? '"' . implode('","', array_map(function($email) {return $email->Subject;}, $allMails)) . '"' : null; |
92
|
|
|
if(trim($negate)) { |
93
|
|
|
assertNull($match); |
94
|
|
|
} else { |
95
|
|
|
$msg = sprintf( |
96
|
|
|
'Could not find email %s "%s" titled "%s".', |
97
|
|
|
$direction, |
98
|
|
|
$email, |
99
|
|
|
$subject |
100
|
|
|
); |
101
|
|
|
if($allTitles) { |
|
|
|
|
102
|
|
|
$msg .= ' Existing emails: ' . $allTitles; |
103
|
|
|
} |
104
|
|
|
assertNotNull($match,$msg); |
105
|
|
|
} |
106
|
|
|
$this->lastMatchedEmail = $match; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Example: Given the email should contain "Thank you for registering!". |
111
|
|
|
* Assumes an email has been identified by a previous step, |
112
|
|
|
* e.g. through 'Given there should be an email to "[email protected]"'. |
113
|
|
|
* |
114
|
|
|
* @Given /^the email should (not |)contain "([^"]*)"$/ |
115
|
|
|
*/ |
116
|
|
|
public function thereTheEmailContains($negate, $content) |
117
|
|
|
{ |
118
|
|
|
if(!$this->lastMatchedEmail) { |
119
|
|
|
throw new \LogicException('No matched email found from previous step'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$email = $this->lastMatchedEmail; |
123
|
|
|
$emailContent = null; |
|
|
|
|
124
|
|
|
if($email->Content) { |
125
|
|
|
$emailContent = $email->Content; |
126
|
|
|
} else { |
127
|
|
|
$emailContent = $email->PlainContent; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if(trim($negate)) { |
131
|
|
|
assertNotContains($content, $emailContent); |
132
|
|
|
} else { |
133
|
|
|
assertContains($content, $emailContent); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Example: Given the email contains "Thank you for <strong>registering!<strong>". |
139
|
|
|
* Then the email should contain plain text "Thank you for registering!" |
140
|
|
|
* Assumes an email has been identified by a previous step, |
141
|
|
|
* e.g. through 'Given there should be an email to "[email protected]"'. |
142
|
|
|
* |
143
|
|
|
* @Given /^the email should contain plain text "([^"]*)"$/ |
144
|
|
|
*/ |
145
|
|
|
public function thereTheEmailContainsPlainText($content) |
146
|
|
|
{ |
147
|
|
|
if(!$this->lastMatchedEmail) { |
148
|
|
|
throw new \LogicException('No matched email found from previous step'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$email = $this->lastMatchedEmail; |
152
|
|
|
$emailContent = ($email->Content) ? ($email->Content) : ($email->PlainContent); |
153
|
|
|
$emailPlainText = strip_tags($emailContent); |
154
|
|
|
$emailPlainText = preg_replace("/\h+/", " ", $emailPlainText); |
155
|
|
|
|
156
|
|
|
assertContains($content, $emailPlainText); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)"$/ |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function iGoToInTheEmailTo($linkSelector, $direction, $email) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$to = ($direction == 'to') ? $email : null; |
165
|
|
|
$from = ($direction == 'from') ? $email : null; |
166
|
|
|
$match = $this->mailer->findEmail($to, $from); |
167
|
|
|
assertNotNull($match); |
168
|
|
|
|
169
|
|
|
$crawler = new Crawler($match->Content); |
170
|
|
|
$linkEl = $crawler->selectLink($linkSelector); |
171
|
|
|
assertNotNull($linkEl); |
172
|
|
|
$link = $linkEl->attr('href'); |
173
|
|
|
assertNotNull($link); |
174
|
|
|
|
175
|
|
|
return new Step\When(sprintf('I go to "%s"', $link)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @When /^I click on the "([^"]*)" link in the email (to|from) "([^"]*)" titled "([^"]*)"$/ |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function iGoToInTheEmailToTitled($linkSelector, $direction, $email, $title) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$to = ($direction == 'to') ? $email : null; |
184
|
|
|
$from = ($direction == 'from') ? $email : null; |
185
|
|
|
$match = $this->mailer->findEmail($to, $from, $title); |
186
|
|
|
assertNotNull($match); |
187
|
|
|
|
188
|
|
|
$crawler = new Crawler($match->Content); |
189
|
|
|
$linkEl = $crawler->selectLink($linkSelector); |
190
|
|
|
assertNotNull($linkEl); |
191
|
|
|
$link = $linkEl->attr('href'); |
192
|
|
|
assertNotNull($link); |
193
|
|
|
return new Step\When(sprintf('I go to "%s"', $link)); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Assumes an email has been identified by a previous step, |
198
|
|
|
* e.g. through 'Given there should be an email to "[email protected]"'. |
199
|
|
|
* |
200
|
|
|
* @When /^I click on the "([^"]*)" link in the email"$/ |
201
|
|
|
*/ |
202
|
|
|
public function iGoToInTheEmail($linkSelector) |
203
|
|
|
{ |
204
|
|
|
if(!$this->lastMatchedEmail) { |
205
|
|
|
throw new \LogicException('No matched email found from previous step'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$match = $this->lastMatchedEmail; |
209
|
|
|
$crawler = new Crawler($match->Content); |
210
|
|
|
$linkEl = $crawler->selectLink($linkSelector); |
211
|
|
|
assertNotNull($linkEl); |
212
|
|
|
$link = $linkEl->attr('href'); |
213
|
|
|
assertNotNull($link); |
214
|
|
|
|
215
|
|
|
return new Step\When(sprintf('I go to "%s"', $link)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @Given /^I clear all emails$/ |
220
|
|
|
*/ |
221
|
|
|
public function iClearAllEmails() |
222
|
|
|
{ |
223
|
|
|
$this->lastMatchedEmail = null; |
224
|
|
|
return $this->mailer->clearEmails(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Example: Then the email should contain the following data: |
229
|
|
|
* | row1 | |
230
|
|
|
* | row2 | |
231
|
|
|
* Assumes an email has been identified by a previous step. |
232
|
|
|
* @Then /^the email should (not |)contain the following data:$/ |
233
|
|
|
*/ |
234
|
|
|
public function theEmailContainFollowingData($negate, TableNode $table) { |
235
|
|
|
if(!$this->lastMatchedEmail) { |
236
|
|
|
throw new \LogicException('No matched email found from previous step'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$email = $this->lastMatchedEmail; |
240
|
|
|
$emailContent = null; |
|
|
|
|
241
|
|
|
if($email->Content) { |
242
|
|
|
$emailContent = $email->Content; |
243
|
|
|
} else { |
244
|
|
|
$emailContent = $email->PlainContent; |
245
|
|
|
} |
246
|
|
|
// Convert html content to plain text |
247
|
|
|
$emailContent = strip_tags($emailContent); |
248
|
|
|
$emailContent = preg_replace("/\h+/", " ", $emailContent); |
249
|
|
|
$rows = $table->getRows(); |
250
|
|
|
|
251
|
|
|
// For "should not contain" |
252
|
|
|
if(trim($negate)) { |
253
|
|
|
foreach($rows as $row) { |
254
|
|
|
assertNotContains($row[0], $emailContent); |
255
|
|
|
} |
256
|
|
|
} else { |
257
|
|
|
foreach($rows as $row) { |
258
|
|
|
assertContains($row[0], $emailContent); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @Then /^there should (not |)be an email titled "([^"]*)"$/ |
265
|
|
|
*/ |
266
|
|
|
public function thereIsAnEmailTitled($negate, $subject) |
267
|
|
|
{ |
268
|
|
|
$match = $this->mailer->findEmail(null, null, $subject); |
269
|
|
|
if(trim($negate)) { |
270
|
|
|
assertNull($match); |
271
|
|
|
} else { |
272
|
|
|
$msg = sprintf( |
273
|
|
|
'Could not find email titled "%s".', |
274
|
|
|
$subject |
275
|
|
|
); |
276
|
|
|
assertNotNull($match,$msg); |
277
|
|
|
} |
278
|
|
|
$this->lastMatchedEmail = $match; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @Then /^the email should (not |)be sent from "([^"]*)"$/ |
283
|
|
|
*/ |
284
|
|
View Code Duplication |
public function theEmailSentFrom($negate, $from) |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
if(!$this->lastMatchedEmail) { |
287
|
|
|
throw new \LogicException('No matched email found from previous step'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$match = $this->lastMatchedEmail; |
291
|
|
|
if(trim($negate)) { |
292
|
|
|
assertNotContains($from, $match->From); |
293
|
|
|
} else { |
294
|
|
|
assertContains($from, $match->From); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @Then /^the email should (not |)be sent to "([^"]*)"$/ |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function theEmailSentTo($negate, $to) |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
if(!$this->lastMatchedEmail) { |
304
|
|
|
throw new \LogicException('No matched email found from previous step'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$match = $this->lastMatchedEmail; |
308
|
|
|
if(trim($negate)) { |
309
|
|
|
assertNotContains($to, $match->To); |
310
|
|
|
} else { |
311
|
|
|
assertContains($to, $match->To); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
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: