Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.00% covered (success)
92.00%
23 / 25
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConflictFormValidator
92.00% covered (success)
92.00%
23 / 25
66.67% covered (warning)
66.67%
2 / 3
15.12
0.00% covered (danger)
0.00%
0 / 1
 validateRequest
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 validateSideSelection
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 validateSingleColumnForm
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
6.40
1<?php
2
3namespace TwoColConflict;
4
5use MediaWiki\Request\WebRequest;
6
7/**
8 * @license GPL-2.0-or-later
9 */
10class ConflictFormValidator {
11
12    /**
13     * Check whether inputs are valid.  Note that a POST without conflict fields is considered
14     * valid.
15     *
16     * @param WebRequest $request
17     * @return bool True when valid
18     */
19    public function validateRequest( WebRequest $request ): bool {
20        $contentRows = $request->getArray( 'mw-twocolconflict-split-content' );
21        if ( $contentRows === null ) {
22            // Not a conflict form.
23            return true;
24        }
25        if ( $contentRows === [] ) {
26            // Empty conflict form is bad.
27            return false;
28        }
29
30        $sideSelection = $request->getArray( 'mw-twocolconflict-side-selector', [] );
31        if ( $sideSelection ) {
32            return $this->validateSideSelection( $contentRows, $sideSelection );
33        }
34
35        if ( $request->getBool( 'mw-twocolconflict-single-column-view' ) ) {
36            return $this->validateSingleColumnForm( $contentRows );
37        }
38
39        return false;
40    }
41
42    /**
43     * @param array[] $contentRows
44     * @param string[] $sideSelection
45     *
46     * @return bool
47     */
48    private function validateSideSelection( array $contentRows, array $sideSelection ): bool {
49        foreach ( $contentRows as $num => $row ) {
50            $side = $sideSelection[$num] ?? 'copy';
51            if ( !isset( $row[$side] ) || !is_string( $row[$side] ) ) {
52                return false;
53            }
54        }
55
56        return true;
57    }
58
59    /**
60     * @param array[] $contentRows
61     *
62     * @return bool
63     */
64    private function validateSingleColumnForm( array $contentRows ): bool {
65        foreach ( $contentRows as $num => $row ) {
66            if ( !is_array( $row ) || count( $row ) !== 1 ) {
67                // Must be an array with exactly one column.
68                return false;
69            }
70            $key = key( $row );
71            if ( !in_array( $key, [ 'copy', 'other', 'your' ] ) ) {
72                // Illegal key.
73                return false;
74            }
75            if ( !is_string( $row[$key] ) ) {
76                // Contents must be a plain string.
77                return false;
78            }
79        }
80
81        return true;
82    }
83
84}