Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
13.21% covered (danger)
13.21%
7 / 53
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialEnrollAsMentor
13.21% covered (danger)
13.21%
7 / 53
0.00% covered (danger)
0.00%
0 / 12
205.95
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
53.85% covered (warning)
53.85%
7 / 13
0.00% covered (danger)
0.00%
0 / 1
5.57
 preHtml
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 onSuccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 displayRestrictionError
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace GrowthExperiments\Specials;
4
5use GrowthExperiments\MentorDashboard\MentorTools\IMentorWeights;
6use GrowthExperiments\Mentorship\Provider\IMentorWriter;
7use GrowthExperiments\Mentorship\Provider\MentorProvider;
8use LogicException;
9use MediaWiki\Config\Config;
10use MediaWiki\HTMLForm\HTMLForm;
11use MediaWiki\SpecialPage\FormSpecialPage;
12use MediaWiki\SpecialPage\SpecialPage;
13use MediaWiki\Status\Status;
14use PermissionsError;
15use UserBlockedError;
16
17class SpecialEnrollAsMentor extends FormSpecialPage {
18
19    private Config $wikiConfig;
20    private MentorProvider $mentorProvider;
21    private IMentorWriter $mentorWriter;
22
23    /**
24     * @param Config $wikiConfig
25     * @param MentorProvider $mentorProvider
26     * @param IMentorWriter $mentorWriter
27     */
28    public function __construct(
29        Config $wikiConfig,
30        MentorProvider $mentorProvider,
31        IMentorWriter $mentorWriter
32    ) {
33        parent::__construct( 'EnrollAsMentor', 'enrollasmentor', false );
34
35        $this->wikiConfig = $wikiConfig;
36        $this->mentorProvider = $mentorProvider;
37        $this->mentorWriter = $mentorWriter;
38    }
39
40    /** @inheritDoc */
41    protected function getGroupName() {
42        return 'growth-tools';
43    }
44
45    /**
46     * @inheritDoc
47     */
48    public function doesWrites() {
49        return true;
50    }
51
52    /**
53     * @inheritDoc
54     */
55    protected function getDisplayFormat() {
56        return 'ooui';
57    }
58
59    /**
60     * @inheritDoc
61     */
62    public function getDescription() {
63        return $this->msg( 'growthexperiments-mentorship-enrollasmentor-title' );
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function execute( $par ) {
70        $this->requireNamedUser();
71
72        if ( $this->mentorProvider->isMentor( $this->getUser() ) ) {
73            $this->getOutput()->redirect(
74                SpecialPage::getTitleFor( 'MentorDashboard' )->getLocalURL()
75            );
76        }
77
78        if ( $this->mentorWriter->isBlocked( $this->getUser() ) ) {
79            $block = $this->getUser()->getBlock();
80            if ( !$block ) {
81                throw new LogicException(
82                    'IMentorWriter::isBlocked returns true, but User::getBlock returns null'
83                );
84            }
85            throw new UserBlockedError( $block );
86        }
87
88        parent::execute( $par );
89    }
90
91    /**
92     * @inheritDoc
93     */
94    protected function preHtml() {
95        return $this->msg( 'growthexperiments-mentorship-enrollasmentor-pretext' )->parse();
96    }
97
98    /**
99     * @inheritDoc
100     */
101    protected function alterForm( HTMLForm $form ) {
102        $form->setSubmitText(
103            $this->msg( 'growthexperiments-mentorship-enrollasmentor-enroll' )->text()
104        );
105    }
106
107    /**
108     * @inheritDoc
109     */
110    protected function getFormFields() {
111        return [
112            'message' => [
113                'type' => 'text',
114                'label-message' => 'growthexperiments-mentorship-enrollasmentor-form-message',
115                'help-message' => 'growthexperiments-mentorship-enrollasmentor-form-message-help',
116                'maxlength' => MentorProvider::INTRO_TEXT_LENGTH,
117            ],
118            'weight' => [
119                'type' => 'hidden',
120                'default' => IMentorWeights::WEIGHT_NORMAL,
121            ],
122        ];
123    }
124
125    /**
126     * @inheritDoc
127     */
128    public function onSubmit( array $data ) {
129        $mentor = $this->mentorProvider->newMentorFromUserIdentity( $this->getUser() );
130        $mentor->setIntroText( $data['message'] !== '' ? $data['message'] : null );
131        $mentor->setWeight( (int)$data['weight'] );
132
133        return Status::wrap( $this->mentorWriter->addMentor(
134            $mentor,
135            $this->getUser(),
136            ''
137        ) );
138    }
139
140    /**
141     * @inheritDoc
142     */
143    public function onSuccess() {
144        $this->getOutput()->addWikiMsg( 'growthexperiments-mentorship-enrollasmentor-success' );
145    }
146
147    protected function displayRestrictionError() {
148        if ( !$this->wikiConfig->get( 'GEMentorshipAutomaticEligibility' ) ) {
149            parent::displayRestrictionError();
150        }
151
152        throw new PermissionsError( $this->mRestriction, [ [
153            'growthexperiments-mentorship-enrollasmentor-error-not-autoeligible',
154            $this->wikiConfig->get( 'GEMentorshipMinimumEditcount' ),
155            $this->wikiConfig->get( 'GEMentorshipMinimumAge' ),
156        ] ] );
157    }
158}