|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\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
|
|
|
* @package silverstripe-console |
|
16
|
|
|
* @author Robbie Averill <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class ChangeGroupsCommandTest extends AbstractCommandTest |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Ensure a clean slate for each test run |
|
22
|
|
|
* |
|
23
|
|
|
* {@inheritDoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::setUp(); |
|
28
|
|
|
Member::get()->removeAll(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getTestCommand() |
|
35
|
|
|
{ |
|
36
|
|
|
return 'member:change-groups'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Test that an error message is returned if the Member does not exist |
|
41
|
|
|
* |
|
42
|
|
|
* @covers ::execute |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testReportMemberNotFound() |
|
45
|
|
|
{ |
|
46
|
|
|
$tester = $this->executeTest(['email' => '[email protected]']); |
|
47
|
|
|
$this->assertContains('Member with email "[email protected]" was not found', $tester->getDisplay()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Test that when a Group is chosen from the multiselect list, the user is assigned to that Group or Groups |
|
52
|
|
|
* |
|
53
|
|
|
* @covers ::execute |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testAddToGroups() |
|
56
|
|
|
{ |
|
57
|
|
|
$member = $this->createMember(); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$this->mockQuestionHelper(); |
|
60
|
|
|
$tester = $this->executeTest(['email' => '[email protected]']); |
|
61
|
|
|
$this->assertContains('Groups updated.', $tester->getDisplay()); |
|
62
|
|
|
|
|
63
|
|
|
$memberCodes = Member::get() |
|
64
|
|
|
->filter('Email', '[email protected]') |
|
65
|
|
|
->first() |
|
66
|
|
|
->Groups() |
|
67
|
|
|
->column('Code'); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertSame(['content-authors'], $memberCodes); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Creates a dummy user for testing with |
|
74
|
|
|
* |
|
75
|
|
|
* @return Member |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function createMember() |
|
78
|
|
|
{ |
|
79
|
|
|
$member = Member::create(); |
|
80
|
|
|
$member->Email = '[email protected]'; |
|
81
|
|
|
$member->Password = 'opensesame'; |
|
82
|
|
|
$member->write(); |
|
83
|
|
|
return $member; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Mock a QuestionHelper and tell it to return a predefined choice for which Group to assign |
|
88
|
|
|
* |
|
89
|
|
|
* @return QuestionHelper |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function mockQuestionHelper() |
|
92
|
|
|
{ |
|
93
|
|
|
$mock = $this |
|
94
|
|
|
->getMockBuilder(QuestionHelper::class) |
|
95
|
|
|
->setMethods(['ask']) |
|
96
|
|
|
->getMock(); |
|
97
|
|
|
|
|
98
|
|
|
$mock |
|
99
|
|
|
->expects($this->once()) |
|
100
|
|
|
->method('ask') |
|
101
|
|
|
->with( |
|
102
|
|
|
$this->isInstanceOf(InputInterface::class), |
|
103
|
|
|
$this->isInstanceOf(OutputInterface::class), |
|
104
|
|
|
$this->callback( |
|
105
|
|
|
function ($argument) { |
|
106
|
|
|
return $argument instanceof ChoiceQuestion |
|
107
|
|
|
&& $argument->isMultiselect() |
|
108
|
|
|
&& !empty($argument->getChoices()); |
|
109
|
|
|
} |
|
110
|
|
|
) |
|
111
|
|
|
) |
|
112
|
|
|
->willReturn(['content-authors']); |
|
113
|
|
|
|
|
114
|
|
|
$this->command->getApplication()->getHelperSet()->set($mock, 'question'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.