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    private Authority $performer;
39    private Title $title;
40    private string $newContentModel;
41    private string $result;
42
43    /**
44     * @param Authority $performer
45     * @param Title $title
46     * @param string $newContentModel
47     */
48    public function __construct(
49        Authority $performer,
50        Title $title,
51        string $newContentModel
52    ) {
53        $this->performer = $performer;
54        $this->title = $title;
55        $this->newContentModel = $newContentModel;
56    }
57
58    public function checkConstraint(): string {
59        if ( $this->newContentModel === $this->title->getContentModel() ) {
60            // No change
61            $this->result = self::CONSTRAINT_PASSED;
62            return self::CONSTRAINT_PASSED;
63        }
64
65        if ( !$this->performer->isAllowed( 'editcontentmodel' ) ) {
66            $this->result = self::CONSTRAINT_FAILED;
67            return self::CONSTRAINT_FAILED;
68        }
69
70        // Make sure the user can edit the page under the new content model too
71        $titleWithNewContentModel = clone $this->title;
72        $titleWithNewContentModel->setContentModel( $this->newContentModel );
73
74        $canEditModel = $this->performer->authorizeWrite(
75            'editcontentmodel',
76            $titleWithNewContentModel
77        );
78
79        if (
80            !$canEditModel
81            || !$this->performer->authorizeWrite( 'edit', $titleWithNewContentModel )
82        ) {
83            $this->result = self::CONSTRAINT_FAILED;
84            return self::CONSTRAINT_FAILED;
85        }
86
87        $this->result = self::CONSTRAINT_PASSED;
88        return self::CONSTRAINT_PASSED;
89    }
90
91    public function getLegacyStatus(): StatusValue {
92        $statusValue = StatusValue::newGood();
93
94        if ( $this->result === self::CONSTRAINT_FAILED ) {
95            $statusValue->setResult( false, self::AS_NO_CHANGE_CONTENT_MODEL );
96        }
97
98        return $statusValue;
99    }
100
101}