1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Tests\Command\Member; |
4
|
|
|
|
5
|
|
|
use SilverLeague\Console\Tests\Command\AbstractCommandTest; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Change a member's assigned groups |
14
|
|
|
* |
15
|
|
|
* @coversDefaultClass \SilverLeague\Console\Command\Member\ChangeGroupsCommand |
16
|
|
|
* @package silverstripe-console |
17
|
|
|
* @author Robbie Averill <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ChangeGroupsCommandTest extends AbstractCommandTest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Delete fixtured members after tests have run |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
protected function tearDown() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
parent::tearDown(); |
27
|
|
|
|
28
|
|
|
$testMember = Member::get()->filter(['Email' => '[email protected]'])->first(); |
29
|
|
|
if ($testMember && $testMember->exists()) { |
30
|
|
|
$testMember->delete(); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
protected function getTestCommand() |
38
|
|
|
{ |
39
|
|
|
return 'member:change-groups'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Test that an error message is returned if the Member does not exist |
44
|
|
|
* |
45
|
|
|
* @covers ::execute |
46
|
|
|
*/ |
47
|
|
|
public function testReportMemberNotFound() |
48
|
|
|
{ |
49
|
|
|
$tester = $this->executeTest(['email' => '[email protected]']); |
50
|
|
|
$this->assertContains('Member with email "[email protected]" was not found', $tester->getDisplay()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test that when a Group is chosen from the multiselect list, the user is assigned to that Group or Groups |
55
|
|
|
* |
56
|
|
|
* @covers ::execute |
57
|
|
|
*/ |
58
|
|
|
public function testAddToGroups() |
59
|
|
|
{ |
60
|
|
|
$member = $this->createMember(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->mockQuestionHelper(); |
63
|
|
|
$tester = $this->executeTest(['email' => '[email protected]']); |
64
|
|
|
$this->assertContains('Groups updated.', $tester->getDisplay()); |
65
|
|
|
|
66
|
|
|
$memberCodes = Member::get() |
67
|
|
|
->filter('Email', '[email protected]') |
68
|
|
|
->first() |
69
|
|
|
->Groups() |
70
|
|
|
->column('Code'); |
71
|
|
|
|
72
|
|
|
$this->assertSame(['content-authors'], $memberCodes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Creates a dummy user for testing with |
77
|
|
|
* |
78
|
|
|
* @return Member |
79
|
|
|
*/ |
80
|
|
|
protected function createMember() |
81
|
|
|
{ |
82
|
|
|
$member = Member::create(); |
83
|
|
|
$member->Email = '[email protected]'; |
84
|
|
|
$member->Password = 'Opensesame1'; |
85
|
|
|
$member->write(); |
86
|
|
|
return $member; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Mock a QuestionHelper and tell it to return a predefined choice for which Group to assign |
91
|
|
|
* |
92
|
|
|
* @return QuestionHelper |
93
|
|
|
*/ |
94
|
|
|
protected function mockQuestionHelper() |
95
|
|
|
{ |
96
|
|
|
$mock = $this |
97
|
|
|
->getMockBuilder(QuestionHelper::class) |
98
|
|
|
->setMethods(['ask']) |
99
|
|
|
->getMock(); |
100
|
|
|
|
101
|
|
|
$mock |
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('ask') |
104
|
|
|
->with( |
105
|
|
|
$this->isInstanceOf(InputInterface::class), |
106
|
|
|
$this->isInstanceOf(OutputInterface::class), |
107
|
|
|
$this->callback( |
108
|
|
|
function ($argument) { |
109
|
|
|
return $argument instanceof ChoiceQuestion |
110
|
|
|
&& $argument->isMultiselect() |
111
|
|
|
&& !empty($argument->getChoices()); |
112
|
|
|
} |
113
|
|
|
) |
114
|
|
|
) |
115
|
|
|
->willReturn(['content-authors']); |
116
|
|
|
|
117
|
|
|
$this->command->getApplication()->getHelperSet()->set($mock, 'question'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.