Skip to content

Commit 7555790

Browse files
committed
Safer number setting
1 parent b945298 commit 7555790

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

includes/MslsBlogCollection.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ public static function get_configured_blog_description( int $blog_id, $descripti
126126
* @return object[]|\stdClass[]
127127
*/
128128
public function get_blogs_of_reference_user( MslsOptions $options ) {
129-
$reference_user = $options->has_value( 'reference_user' ) ? $options->reference_user : current(
130-
$this->get_users(
131-
'ID',
132-
1
133-
)
134-
);
129+
$reference_user = $options->has_value( 'reference_user' ) ?
130+
$options->reference_user :
131+
current( $this->get_users( 'ID', 1 ) );
135132

136133
$blogs = get_blogs_of_user( $reference_user );
137134
foreach ( $blogs as $key => $blog ) {
@@ -306,17 +303,16 @@ public function get_filtered( bool $filter = false ): array {
306303
*
307304
* @return array<string, int>
308305
*/
309-
public function get_users( $fields = 'all', int $number = 0 ): array {
306+
public function get_users( $fields = 'all', int $number = MslsAdmin::MAX_REFERENCE_USERS ): array {
310307
$args = array(
311308
'blog_id' => $this->current_blog_id,
312309
'orderby' => 'registered',
313310
'fields' => $fields,
311+
'number' => $number > 0 ? $number : MslsAdmin::MAX_REFERENCE_USERS,
314312
'count_total' => false,
315313
);
316314

317-
if ( $number > 1 ) {
318-
$args['number'] = $number;
319-
315+
if ( $number !== 1 ) { // Check total users only if not fetching a single user
320316
$user_count = count_users();
321317
if ( isset( $user_count['total_users'] ) && $user_count['total_users'] > $number ) {
322318
/* translators: %s: maximum number of users */

tests/phpunit/TestMslsBlogCollection.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
final class TestMslsBlogCollection extends MslsUnitTestCase {
1111

12+
const TOTAL_USERS = 3210;
13+
1214
protected function setUp(): void {
1315
parent::setUp(); // TODO: Change the autogenerated stub
1416

15-
Functions\when( 'count_users' )->justReturn( array( 'total_users' => 3210 ) );
17+
Functions\when( 'count_users' )->justReturn( array( 'total_users' => self::TOTAL_USERS ) );
1618

1719
$options = \Mockery::mock( MslsOptions::class );
1820
$options->shouldReceive( 'get_order' )->andReturn( 'description' );
@@ -193,12 +195,20 @@ public function test_get_filtered(): void {
193195
$this->assertIsArray( $obj->get_filtered( true ) );
194196
}
195197

196-
public function test_get_users(): void {
198+
public function test_get_users_single(): void {
199+
Functions\expect( 'get_site_option' )->once()->andReturn( array() );
200+
201+
$obj = new MslsBlogCollection();
202+
203+
$this->assertIsArray( $obj->get_users( array( 'ID' ), 1 ) );
204+
}
205+
206+
public function test_get_users_massive(): void {
197207
Functions\expect( 'get_site_option' )->once()->andReturn( array() );
198208

199209
$obj = new MslsBlogCollection();
200210

201-
$this->assertIsArray( $obj->get_users() );
211+
$this->assertIsArray( $obj->get_users( array( 'ID' ), self::TOTAL_USERS ) );
202212
}
203213

204214
public function test_get_users_max() {

0 commit comments

Comments
 (0)