MediaWiki  master
ApiQueryBlocksTest.php
Go to the documentation of this file.
1 <?php
2 
6 
15 
16  protected $tablesUsed = [
17  'ipblocks',
18  'ipblocks_restrictions',
19  ];
20 
21  public function testExecute() {
22  list( $data ) = $this->doApiRequest( [
23  'action' => 'query',
24  'list' => 'blocks',
25  ] );
26  $this->assertEquals( [ 'batchcomplete' => true, 'query' => [ 'blocks' => [] ] ], $data );
27  }
28 
29  public function testExecuteBlock() {
30  $badActor = $this->getTestUser()->getUser();
31  $sysop = $this->getTestSysop()->getUser();
32 
33  $block = new DatabaseBlock( [
34  'address' => $badActor->getName(),
35  'user' => $badActor->getId(),
36  'by' => $sysop->getId(),
37  'expiry' => 'infinity',
38  ] );
39 
40  $block->insert();
41 
42  list( $data ) = $this->doApiRequest( [
43  'action' => 'query',
44  'list' => 'blocks',
45  ] );
46  $this->arrayHasKey( 'query', $data );
47  $this->arrayHasKey( 'blocks', $data['query'] );
48  $this->assertCount( 1, $data['query']['blocks'] );
49  $subset = [
50  'id' => $block->getId(),
51  'user' => $badActor->getName(),
52  'expiry' => $block->getExpiry(),
53  ];
54  $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
55  }
56 
57  public function testExecuteSitewide() {
58  $badActor = $this->getTestUser()->getUser();
59  $sysop = $this->getTestSysop()->getUser();
60 
61  $block = new DatabaseBlock( [
62  'address' => $badActor->getName(),
63  'user' => $badActor->getId(),
64  'by' => $sysop->getId(),
65  'ipb_expiry' => 'infinity',
66  'ipb_sitewide' => 1,
67  ] );
68 
69  $block->insert();
70 
71  list( $data ) = $this->doApiRequest( [
72  'action' => 'query',
73  'list' => 'blocks',
74  ] );
75  $this->arrayHasKey( 'query', $data );
76  $this->arrayHasKey( 'blocks', $data['query'] );
77  $this->assertCount( 1, $data['query']['blocks'] );
78  $subset = [
79  'id' => $block->getId(),
80  'user' => $badActor->getName(),
81  'expiry' => $block->getExpiry(),
82  'partial' => !$block->isSitewide(),
83  ];
84  $this->assertArraySubset( $subset, $data['query']['blocks'][0] );
85  }
86 
87  public function testExecuteRestrictions() {
88  $badActor = $this->getTestUser()->getUser();
89  $sysop = $this->getTestSysop()->getUser();
90 
91  $block = new DatabaseBlock( [
92  'address' => $badActor->getName(),
93  'user' => $badActor->getId(),
94  'by' => $sysop->getId(),
95  'expiry' => 'infinity',
96  'sitewide' => 0,
97  ] );
98 
99  $block->insert();
100 
101  $subset = [
102  'id' => $block->getId(),
103  'user' => $badActor->getName(),
104  'expiry' => $block->getExpiry(),
105  ];
106 
107  $title = 'Lady Macbeth';
108  $pageData = $this->insertPage( $title );
109  $pageId = $pageData['id'];
110 
111  $this->db->insert( 'ipblocks_restrictions', [
112  'ir_ipb_id' => $block->getId(),
113  'ir_type' => PageRestriction::TYPE_ID,
114  'ir_value' => $pageId,
115  ] );
116  // Page that has been deleted.
117  $this->db->insert( 'ipblocks_restrictions', [
118  'ir_ipb_id' => $block->getId(),
119  'ir_type' => PageRestriction::TYPE_ID,
120  'ir_value' => 999999,
121  ] );
122  $this->db->insert( 'ipblocks_restrictions', [
123  'ir_ipb_id' => $block->getId(),
124  'ir_type' => NamespaceRestriction::TYPE_ID,
125  'ir_value' => NS_USER_TALK,
126  ] );
127  $this->db->insert( 'ipblocks_restrictions', [
128  'ir_ipb_id' => $block->getId(),
129  'ir_type' => 3,
130  'ir_value' => 4,
131  ] );
132 
133  // Test without requesting restrictions.
134  list( $data ) = $this->doApiRequest( [
135  'action' => 'query',
136  'list' => 'blocks',
137  ] );
138  $this->arrayHasKey( 'query', $data );
139  $this->arrayHasKey( 'blocks', $data['query'] );
140  $this->assertCount( 1, $data['query']['blocks'] );
141  $flagSubset = array_merge( $subset, [
142  'partial' => !$block->isSitewide(),
143  ] );
144  $this->assertArraySubset( $flagSubset, $data['query']['blocks'][0] );
145  $this->assertArrayNotHasKey( 'restrictions', $data['query']['blocks'][0] );
146 
147  // Test requesting the restrictions.
148  list( $data ) = $this->doApiRequest( [
149  'action' => 'query',
150  'list' => 'blocks',
151  'bkprop' => 'id|user|expiry|restrictions'
152  ] );
153  $this->arrayHasKey( 'query', $data );
154  $this->arrayHasKey( 'blocks', $data['query'] );
155  $this->assertCount( 1, $data['query']['blocks'] );
156  $restrictionsSubset = array_merge( $subset, [
157  'restrictions' => [
158  'pages' => [
159  [
160  'id' => $pageId,
161  'ns' => 0,
162  'title' => $title,
163  ],
164  ],
165  'namespaces' => [
166  NS_USER_TALK,
167  ],
168  ],
169  ] );
170  $this->assertArraySubset( $restrictionsSubset, $data['query']['blocks'][0] );
171  $this->assertArrayNotHasKey( 'partial', $data['query']['blocks'][0] );
172  }
173 }
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
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
insert( $dbw=null)
Insert a block into the block table.
static getTestSysop()
Convenience method for getting an immutable admin test user.
insertPage( $pageName, $text='Sample page for unit test.', $namespace=null, User $user=null)
Insert a new page.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
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
const NS_USER_TALK
Definition: Defines.php:63
API Database medium.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.