Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ContentModelChangeConstraint
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 checkConstraint
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
5
 getLegacyStatus
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\EditPage\Constraint;
22
23use MediaWiki\Permissions\Authority;
24use MediaWiki\Title\Title;
25use StatusValue;
26
27/**
28 * Verify user permissions if changing content model:
29 *    Must have editcontentmodel rights
30 *    Must be able to edit under the new content model
31 *
32 * @since 1.36
33 * @internal
34 * @author DannyS712
35 */
36class ContentModelChangeConstraint implements IEditConstraint {
37
38    /** @var Authority */
39    private $performer;
40
41    /** @var Title */
42    private $title;
43
44    /** @var string */
45    private $newContentModel;
46
47    /** @var string|null */
48    private $result;
49
50    /**
51     * @param Authority $performer
52     * @param Title $title
53     * @param string $newContentModel
54     */
55    public function __construct(
56        Authority $performer,
57        Title $title,
58        string $newContentModel
59    ) {
60        $this->performer = $performer;
61        $this->title = $title;
62        $this->newContentModel = $newContentModel;
63    }
64
65    public function checkConstraint(): string {
66        if ( $this->newContentModel === $this->title->getContentModel() ) {
67            // No change
68            $this->result = self::CONSTRAINT_PASSED;
69            return self::CONSTRAINT_PASSED;
70        }
71
72        if ( !$this->performer->isAllowed( 'editcontentmodel' ) ) {
73            $this->result = self::CONSTRAINT_FAILED;
74            return self::CONSTRAINT_FAILED;
75        }
76
77        // Make sure the user can edit the page under the new content model too
78        $titleWithNewContentModel = clone $this->title;
79        $titleWithNewContentModel->setContentModel( $this->newContentModel );
80
81        $canEditModel = $this->performer->authorizeWrite(
82            'editcontentmodel',
83            $titleWithNewContentModel
84        );
85
86        if (
87            !$canEditModel
88            || !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel )
89        ) {
90            $this->result = self::CONSTRAINT_FAILED;
91            return self::CONSTRAINT_FAILED;
92        }
93
94        $this->result = self::CONSTRAINT_PASSED;
95        return self::CONSTRAINT_PASSED;
96    }
97
98    public function getLegacyStatus(): StatusValue {
99        $statusValue = StatusValue::newGood();
100
101        if ( $this->result === self::CONSTRAINT_FAILED ) {
102            $statusValue->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
103        }
104
105        return $statusValue;
106    }
107
108}