Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ValidationHooks
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onJsonValidateSave
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace MediaWiki\Extension\CommunityConfiguration\Hooks;
4
5use MediaWiki\Content\Hook\JsonValidateSaveHook;
6use MediaWiki\Content\JsonContent;
7use MediaWiki\Extension\CommunityConfiguration\Provider\ConfigurationProviderFactory;
8use MediaWiki\Extension\CommunityConfiguration\Store\WikiPageStore;
9use MediaWiki\Page\PageIdentity;
10use StatusValue;
11
12class ValidationHooks implements JsonValidateSaveHook {
13
14    private ConfigurationProviderFactory $factory;
15
16    public function __construct( ConfigurationProviderFactory $factory ) {
17        $this->factory = $factory;
18    }
19
20    /**
21     * @inheritDoc
22     */
23    public function onJsonValidateSave(
24        JsonContent $content,
25        PageIdentity $pageIdentity,
26        StatusValue $status
27    ) {
28        foreach ( $this->factory->getSupportedKeys() as $providerName ) {
29            $provider = $this->factory->newProvider( $providerName );
30            $store = $provider->getStore();
31            if ( !$store instanceof WikiPageStore ) {
32                // does not make sense to do any validation here
33                continue;
34            }
35
36            if ( $pageIdentity->isSamePageAs( $store->getConfigurationTitle() ) ) {
37                $validator = $provider->getValidator();
38                $result = $validator->validateStrictly(
39                    WikiPageStore::removeVersionDataFromStatus( $content->getData() )->getValue()
40                );
41                $status->merge( $result );
42            }
43        }
44    }
45}