Debian Codegraph @

tests/phpunit/integration/includes/user/TempUser/TempUserTestTrait.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User\TempUser;
4
5 use MediaWiki\MainConfigNames;
6
7 /**
8 * Helper trait for defining the temporary user configuration settings for tests.
9 * This can only be used for test classes that extend MediaWikiIntegrationTestCase.
10 *
11 * @stable to use
12 * @since 1.42
13 */
14 trait TempUserTestTrait {
15
16 /**
17 * Array of default configuration to use in tests.
18 *
19 * @return array
20 */
21 private function getTempAccountConfigTestDefaults(): array {
22 return [
23 'expireAfterDays' => 90,
24 'notifyBeforeExpirationDays' => 10,
25 'actions' => [ 'edit' ],
26 'genPattern' => '~$1',
27 'reservedPattern' => '~$1',
28 'serialProvider' => [ 'type' => 'local', 'useYear' => true ],
29 'serialMapping' => [ 'type' => 'plain-numeric' ],
30 ];
31 }
32
33 /**
34 * Loads configuration that enables the automatic creation of temporary accounts using the defaults
35 * for the generation pattern and match pattern.
36 *
37 * @param array $configOverrides Specify overrides to the default wgAutoCreateTempUser configuration
38 * setting (all values are the default except 'enabled' which is set to true).
39 * @since 1.42
40 */
41 protected function enableAutoCreateTempUser( array $configOverrides = [] ): void {
42 $configOverrides['enabled'] = true;
43 $this->overrideConfigValue(
44 MainConfigNames::TempAccountNameAcquisitionThrottle,
45 [ 'count' => 0, 'seconds' => 86400 ]
46 );
47 $this->overrideConfigValue(
48 MainConfigNames::AutoCreateTempUser,
49 array_merge( $this->getTempAccountConfigTestDefaults(), $configOverrides )
50 );
51 $this->setGroupPermissions( '*', 'createaccount', true );
52 }
53
54 /**
55 * Disables the automatic creation of temporary accounts for the test.
56 *
57 * This is done to avoid exceptions when a test or the code being tested creates an actor for an IP address.
58 *
59 * @param array $configOverrides Specify overrides to the default wgAutoCreateTempUser configuration
60 * setting (all values are the default except 'enabled' which is set to false).
61 * @since 1.42
62 */
63 protected function disableAutoCreateTempUser( array $configOverrides = [] ): void {
64 $configOverrides['enabled'] = false;
65 $this->overrideConfigValue(
66 MainConfigNames::AutoCreateTempUser,
67 array_merge( $this->getTempAccountConfigTestDefaults(), $configOverrides )
68 );
69 }
70
71 /**
72 * Defined to ensure that the class has the overrideConfigValue method that we can use.
73 *
74 * @see \MediaWikiIntegrationTestCase::overrideConfigValue
75 *
76 * @param string $key
77 * @param mixed $value
78 */
79 abstract protected function overrideConfigValue( string $key, $value );
80 }