Skip to content

Commit be75ea8

Browse files
Networks and Sites: Set WP_Network properties via setters upon creation.
This ensures that `WP_Network::$id` is stored internally as `int`, to match the documented type. Follow-up to [37870]. Props ironprogrammer, scottculverhouse, spacedmonkey, SergeyBiryukov. See #62035. git-svn-id: https://develop.svn.wordpress.org/trunk@59020 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a78540b commit be75ea8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/wp-includes/class-wp-network.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static function get_instance( $network_id ) {
131131
*/
132132
public function __construct( $network ) {
133133
foreach ( get_object_vars( $network ) as $key => $value ) {
134-
$this->$key = $value;
134+
$this->__set( $key, $value );
135135
}
136136

137137
$this->_set_site_name();

tests/phpunit/tests/multisite/network.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public function get_main_network_id() {
127127

128128
/**
129129
* @ticket 37050
130+
*
131+
* @covers WP_Network::__get
130132
*/
131133
public function test_wp_network_object_id_property_is_int() {
132134
$id = self::factory()->network->create();
@@ -136,6 +138,28 @@ public function test_wp_network_object_id_property_is_int() {
136138
$this->assertSame( (int) $id, $network->id );
137139
}
138140

141+
/**
142+
* Tests that the `WP_Network::$id` property is stored as int.
143+
*
144+
* Uses reflection to access the private property.
145+
* Differs from using the public getter method, which casts to int.
146+
*
147+
* @ticket 62035
148+
*
149+
* @covers WP_Network::__construct
150+
*/
151+
public function test_wp_network_object_id_property_stored_as_int() {
152+
$id = self::factory()->network->create();
153+
154+
$network = WP_Network::get_instance( $id );
155+
156+
$reflection = new ReflectionObject( $network );
157+
$property = $reflection->getProperty( 'id' );
158+
$property->setAccessible( true );
159+
160+
$this->assertSame( (int) $id, $property->getValue( $network ) );
161+
}
162+
139163
/**
140164
* @ticket 22917
141165
*/

0 commit comments

Comments
 (0)