Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
47.62% |
20 / 42 |
|
31.25% |
5 / 16 |
CRAP | |
0.00% |
0 / 1 |
SpecialQuitMentorship | |
47.62% |
20 / 42 |
|
31.25% |
5 / 16 |
58.54 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMessagePrefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
requireMentorDashboardEnabled | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
execute | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
1.30 | |||
getFormFields | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
alterForm | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
preHtml | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
onSubmit | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
onSuccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
userCanExecute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
displayRestrictionError | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Specials; |
4 | |
5 | use ErrorPageError; |
6 | use GrowthExperiments\Mentorship\MentorRemover; |
7 | use GrowthExperiments\Mentorship\Provider\MentorProvider; |
8 | use MediaWiki\HTMLForm\HTMLForm; |
9 | use MediaWiki\SpecialPage\FormSpecialPage; |
10 | use MediaWiki\User\User; |
11 | use PermissionsError; |
12 | |
13 | class SpecialQuitMentorship extends FormSpecialPage { |
14 | |
15 | private MentorProvider $mentorProvider; |
16 | private MentorRemover $mentorRemover; |
17 | |
18 | /** |
19 | * @param MentorProvider $mentorProvider |
20 | * @param MentorRemover $mentorRemover |
21 | */ |
22 | public function __construct( |
23 | MentorProvider $mentorProvider, |
24 | MentorRemover $mentorRemover |
25 | ) { |
26 | parent::__construct( 'QuitMentorship', '', false ); |
27 | |
28 | $this->mentorProvider = $mentorProvider; |
29 | $this->mentorRemover = $mentorRemover; |
30 | } |
31 | |
32 | /** @inheritDoc */ |
33 | protected function getGroupName() { |
34 | return 'growth-tools'; |
35 | } |
36 | |
37 | /** |
38 | * @inheritDoc |
39 | */ |
40 | public function doesWrites() { |
41 | return true; |
42 | } |
43 | |
44 | /** |
45 | * @inheritDoc |
46 | */ |
47 | protected function getDisplayFormat() { |
48 | return 'ooui'; |
49 | } |
50 | |
51 | /** |
52 | * @inheritDoc |
53 | */ |
54 | protected function getMessagePrefix() { |
55 | return 'growthexperiments-quit-mentorship'; |
56 | } |
57 | |
58 | /** |
59 | * @inheritDoc |
60 | */ |
61 | public function getDescription() { |
62 | return $this->msg( 'growthexperiments-quit-mentorship-title' ); |
63 | } |
64 | |
65 | /** |
66 | * Check if mentor dashboard is enabled via GEMentorDashboardEnabled |
67 | * |
68 | * @return bool |
69 | */ |
70 | private function isEnabled(): bool { |
71 | return $this->getConfig()->get( 'GEMentorDashboardEnabled' ); |
72 | } |
73 | |
74 | /** |
75 | * Ensure mentor dashboard feature flag is on |
76 | * |
77 | * @throws ErrorPageError |
78 | */ |
79 | private function requireMentorDashboardEnabled() { |
80 | if ( !$this->isEnabled() ) { |
81 | // Mentor dashboard is disabled, display a meaningful restriction error |
82 | throw new ErrorPageError( |
83 | 'growthexperiments-quit-mentorship-title', |
84 | 'growthexperiments-quit-mentorship-disabled' |
85 | ); |
86 | } |
87 | } |
88 | |
89 | /** |
90 | * @inheritDoc |
91 | */ |
92 | public function execute( $par ) { |
93 | $this->requireMentorDashboardEnabled(); |
94 | $this->requireNamedUser(); |
95 | |
96 | parent::execute( $par ); |
97 | } |
98 | |
99 | /** |
100 | * @inheritDoc |
101 | */ |
102 | protected function getFormFields() { |
103 | return [ |
104 | 'reason' => [ |
105 | 'type' => 'text', |
106 | 'label-message' => 'growthexperiments-quit-mentorship-reason' |
107 | ] |
108 | ]; |
109 | } |
110 | |
111 | /** |
112 | * @inheritDoc |
113 | */ |
114 | protected function alterForm( HTMLForm $form ) { |
115 | $form->setSubmitDestructive(); |
116 | $form->setSubmitText( $this->msg( |
117 | 'growthexperiments-quit-mentorship-reassign-mentees-confirm' |
118 | )->text() ); |
119 | } |
120 | |
121 | /** |
122 | * @inheritDoc |
123 | */ |
124 | protected function preHtml() { |
125 | return $this->msg( |
126 | 'growthexperiments-quit-mentorship-has-mentees-pretext' |
127 | )->parseAsBlock(); |
128 | } |
129 | |
130 | /** |
131 | * @inheritDoc |
132 | */ |
133 | public function onSubmit( array $data ) { |
134 | return $this->mentorRemover->removeMentor( |
135 | $this->getUser(), |
136 | $this->getUser(), |
137 | $data['reason'], |
138 | $this->getContext() |
139 | ); |
140 | } |
141 | |
142 | /** |
143 | * @inheritDoc |
144 | */ |
145 | public function onSuccess() { |
146 | $this->getOutput()->addWikiMsg( 'growthexperiments-quit-mentorship-success' ); |
147 | } |
148 | |
149 | /** |
150 | * @inheritDoc |
151 | */ |
152 | public function userCanExecute( User $user ) { |
153 | return $this->mentorProvider->isMentor( $this->getUser() ); |
154 | } |
155 | |
156 | /** |
157 | * @inheritDoc |
158 | */ |
159 | protected function displayRestrictionError() { |
160 | throw new PermissionsError( |
161 | null, |
162 | [ 'growthexperiments-quit-mentorship-error-not-a-mentor' ] |
163 | ); |
164 | } |
165 | } |