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
This commit is contained in:
Sergey Biryukov 2024-09-13 22:10:51 +00:00
parent a78540b088
commit be75ea8e73
2 changed files with 25 additions and 1 deletions

View File

@ -131,7 +131,7 @@ class WP_Network {
*/
public function __construct( $network ) {
foreach ( get_object_vars( $network ) as $key => $value ) {
$this->$key = $value;
$this->__set( $key, $value );
}
$this->_set_site_name();

View File

@ -127,6 +127,8 @@ if ( is_multisite() ) :
/**
* @ticket 37050
*
* @covers WP_Network::__get
*/
public function test_wp_network_object_id_property_is_int() {
$id = self::factory()->network->create();
@ -136,6 +138,28 @@ if ( is_multisite() ) :
$this->assertSame( (int) $id, $network->id );
}
/**
* Tests that the `WP_Network::$id` property is stored as int.
*
* Uses reflection to access the private property.
* Differs from using the public getter method, which casts to int.
*
* @ticket 62035
*
* @covers WP_Network::__construct
*/
public function test_wp_network_object_id_property_stored_as_int() {
$id = self::factory()->network->create();
$network = WP_Network::get_instance( $id );
$reflection = new ReflectionObject( $network );
$property = $reflection->getProperty( 'id' );
$property->setAccessible( true );
$this->assertSame( (int) $id, $property->getValue( $network ) );
}
/**
* @ticket 22917
*/