MediaWiki  1.33.0
ApiQueryBlocksTest.php
Go to the documentation of this file.
1 <?php
2 
5 
14 
15  protected $tablesUsed = [
16  'ipblocks',
17  'ipblocks_restrictions',
18  ];
19 
20  public function testExecute() {
21  list( $data ) = $this->doApiRequest( [
22  'action' => 'query',
23  'list' => 'blocks',
24  ] );
25  $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
26  }
27 
28  public function testExecuteBlock() {
29  $badActor = $this->getTestUser()->getUser();
30  $sysop = $this->getTestSysop()->getUser();
31 
32  $block = new Block( [
33  'address' => $badActor->getName(),
34  'user' => $badActor->getId(),
35  'by' => $sysop->getId(),
36  'expiry' => 'infinity',
37  ] );
38 
39  $block->insert();
40 
41  list( $data ) = $this->doApiRequest( [
42  'action' => 'query',
43  'list' => 'blocks',
44  ] );
45  $this->arrayHasKey( 'query', $data );
46  $this->arrayHasKey( 'blocks', $data['query'] );
47  $this->assertCount( 1, $data['query']['blocks'] );
48  $subset = [
49  'id' => $block->getId(),
50  'user' => $badActor->getName(),
51  'expiry' => $block->getExpiry(),
52  ];
53  $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
54  }
55 
56  public function testExecuteSitewide() {
57  $badActor = $this->getTestUser()->getUser();
58  $sysop = $this->getTestSysop()->getUser();
59 
60  $block = new Block( [
61  'address' => $badActor->getName(),
62  'user' => $badActor->getId(),
63  'by' => $sysop->getId(),
64  'ipb_expiry' => 'infinity',
65  'ipb_sitewide' => 1,
66  ] );
67 
68  $block->insert();
69 
70  list( $data ) = $this->doApiRequest( [
71  'action' => 'query',
72  'list' => 'blocks',
73  ] );
74  $this->arrayHasKey( 'query', $data );
75  $this->arrayHasKey( 'blocks', $data['query'] );
76  $this->assertCount( 1, $data['query']['blocks'] );
77  $subset = [
78  'id' => $block->getId(),
79  'user' => $badActor->getName(),
80  'expiry' => $block->getExpiry(),
81  'partial' => !$block->isSitewide(),
82  ];
83  $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
84  }
85 
86  public function testExecuteRestrictions() {
87  $badActor = $this->getTestUser()->getUser();
88  $sysop = $this->getTestSysop()->getUser();
89 
90  $block = new Block( [
91  'address' => $badActor->getName(),
92  'user' => $badActor->getId(),
93  'by' => $sysop->getId(),
94  'expiry' => 'infinity',
95  'sitewide' => 0,
96  ] );
97 
98  $block->insert();
99 
100  $subset = [
101  'id' => $block->getId(),
102  'user' => $badActor->getName(),
103  'expiry' => $block->getExpiry(),
104  ];
105 
106  $title = 'Lady Macbeth';
107  $pageData = $this->insertPage( $title );
108  $pageId = $pageData['id'];
109 
110  $this->db->insert( 'ipblocks_restrictions', [
111  'ir_ipb_id' => $block->getId(),
112  'ir_type' => PageRestriction::TYPE_ID,
113  'ir_value' => $pageId,
114  ] );
115  // Page that has been deleted.
116  $this->db->insert( 'ipblocks_restrictions', [
117  'ir_ipb_id' => $block->getId(),
118  'ir_type' => PageRestriction::TYPE_ID,
119  'ir_value' => 999999,
120  ] );
121  $this->db->insert( 'ipblocks_restrictions', [
122  'ir_ipb_id' => $block->getId(),
123  'ir_type' => NamespaceRestriction::TYPE_ID,
124  'ir_value' => NS_USER_TALK,
125  ] );
126  $this->db->insert( 'ipblocks_restrictions', [
127  'ir_ipb_id' => $block->getId(),
128  'ir_type' => 3,
129  'ir_value' => 4,
130  ] );
131 
132  // Test without requesting restrictions.
133  list( $data ) = $this->doApiRequest( [
134  'action' => 'query',
135  'list' => 'blocks',
136  ] );
137  $this->arrayHasKey( 'query', $data );
138  $this->arrayHasKey( 'blocks', $data['query'] );
139  $this->assertCount( 1, $data['query']['blocks'] );
140  $flagSubset = array_merge( $subset, [
141  'partial' => !$block->isSitewide(),
142  ] );
143  $this->assertArraySubset( $flagSubset, $data['query']['blocks'][0] );
144  $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
145 
146  // Test requesting the restrictions.
147  list( $data ) = $this->doApiRequest( [
148  'action' => 'query',
149  'list' => 'blocks',
150  'bkprop' => 'id|user|expiry|restrictions'
151  ] );
152  $this->arrayHasKey( 'query', $data );
153  $this->arrayHasKey( 'blocks', $data['query'] );
154  $this->assertCount( 1, $data['query']['blocks'] );
155  $restrictionsSubset = array_merge( $subset, [
156  'restrictions' => [
157  'pages' => [
158  [
159  'id' => $pageId,
160  'ns' => 0,
161  'title' => $title,
162  ],
163  ],
164  'namespaces' => [
165  NS_USER_TALK,
166  ],
167  ],
168  ] );
169  $this->assertArraySubset( $restrictionsSubset, $data['query']['blocks'][0] );
170  $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );
171  }
172 }
ApiQueryBlocksTest
API Database medium.
Definition: ApiQueryBlocksTest.php:13
ApiQueryBlocksTest\testExecuteBlock
testExecuteBlock()
Definition: ApiQueryBlocksTest.php:28
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:180
ApiQueryBlocksTest\testExecute
testExecute()
Definition: ApiQueryBlocksTest.php:20
MediaWikiTestCase\insertPage
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null, User $user=null)
Insert a new page.
Definition: MediaWikiTestCase.php:1222
Block\insert
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:545
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null, $tokenType=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:62
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
ApiQueryBlocksTest\testExecuteSitewide
testExecuteSitewide()
Definition: ApiQueryBlocksTest.php:56
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
ApiTestCase
Definition: ApiTestCase.php:5
MediaWikiTestCase\getTestSysop
static getTestSysop()
Convenience method for getting an immutable admin test user.
Definition: MediaWikiTestCase.php:204
MediaWiki\Block\Restriction\NamespaceRestriction
Definition: NamespaceRestriction.php:25
ApiQueryBlocksTest\$tablesUsed
$tablesUsed
Definition: ApiQueryBlocksTest.php:15
MediaWiki\Block\Restriction\PageRestriction
Definition: PageRestriction.php:25
Block
Definition: Block.php:31
ApiQueryBlocksTest\testExecuteRestrictions
testExecuteRestrictions()
Definition: ApiQueryBlocksTest.php:86