query( "DROP TABLE IF EXISTS `{$table}`" ); $wpdb->query( "CREATE TABLE `{$table}` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `post_id` BIGINT(20) NOT NULL, `shared_key` VARCHAR(255) DEFAULT NULL, `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `uk_entity` (`post_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4" ); } } public static function tearDownAfterClass(): void { global $wpdb; foreach ( array( self::LISTING_TABLE, self::VENDOR_TABLE ) as $table ) { $wpdb->query( "DROP TABLE IF EXISTS `{$table}`" ); } WPDO_Entity_Registry::init(); $ref = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' ); $ref->setAccessible( true ); $ref->setValue( null, null ); } protected function setUp(): void { global $wpdb; foreach ( array( self::LISTING_TABLE, self::VENDOR_TABLE ) as $table ) { $wpdb->query( "TRUNCATE TABLE `{$table}`" ); } $GLOBALS['_wp_options'] = array( WPDO_Entity_Registry::SCOPED_ROUTING_OPTION => '1' ); $GLOBALS['_wp_cache'] = array(); $GLOBALS['_wp_post_types'] = array( 101 => 'hp_listing', 202 => 'hp_vendor', 303 => 'post' ); $GLOBALS['_wp_current_filter'] = 'update_post_metadata'; $tec = new ReflectionProperty( WPDO_Schema_Manager::class, 'table_exists_cache' ); $tec->setAccessible( true ); $tec->setValue( null, array( self::LISTING_TABLE => true, self::VENDOR_TABLE => true ) ); $l1 = new ReflectionProperty( WPDO_Cache_Orchestrator::class, 'l1_cache' ); $l1->setAccessible( true ); $l1->setValue( null, array() ); $mode = new ReflectionProperty( WPDO_Mode_Manager::class, 'cache' ); $mode->setAccessible( true ); $mode->setValue( null, array( 'post' => 'aeav_only', 'user' => 'disabled', 'term' => 'disabled', 'comment' => 'disabled' ) ); WPDO_Entity_Registry::init(); WPDO_Entity_Registry::register_adapter( 'post', new WPDO_Adapter_Post() ); WPDO_Entity_Registry::register_group( 'post', 'scope_listing', array( array( 'key' => 'shared_key', 'type' => 'text' ) ), array( 'post_types' => array( 'hp_listing' ) ) ); WPDO_Entity_Registry::register_group( 'post', 'scope_vendor', array( array( 'key' => 'shared_key', 'type' => 'text' ) ), array( 'post_types' => array( 'hp_vendor' ) ) ); } private function value_in( string $table, int $post_id ): ?string { global $wpdb; return $wpdb->get_var( $wpdb->prepare( "SELECT shared_key FROM `{$table}` WHERE post_id = %d", $post_id ) ); } public function test_each_post_type_writes_to_its_own_group_table(): void { WPDO_Hook_Bus::intercept_update( null, 101, 'shared_key', 'listing value', '' ); WPDO_Hook_Bus::intercept_update( null, 202, 'shared_key', 'vendor value', '' ); $this->assertSame( 'listing value', $this->value_in( self::LISTING_TABLE, 101 ) ); $this->assertNull( $this->value_in( self::VENDOR_TABLE, 101 ) ); $this->assertSame( 'vendor value', $this->value_in( self::VENDOR_TABLE, 202 ) ); } public function test_reads_come_from_the_post_types_group_table(): void { global $wpdb; $wpdb->insert( self::LISTING_TABLE, array( 'post_id' => 101, 'shared_key' => 'from listing table' ) ); $wpdb->insert( self::VENDOR_TABLE, array( 'post_id' => 101, 'shared_key' => 'stray copy' ) ); $this->assertSame( array( 'from listing table' ), WPDO_Hook_Bus::intercept_get( null, 101, 'shared_key', true, 'post' ) ); } public function test_other_post_types_fall_through_to_native_meta(): void { $this->assertNull( WPDO_Hook_Bus::intercept_update( null, 303, 'shared_key', 'x', '' ) ); $this->assertNull( WPDO_Hook_Bus::intercept_get( null, 303, 'shared_key', true, 'post' ) ); } public function test_without_the_option_routing_stays_legacy(): void { $GLOBALS['_wp_options'][ WPDO_Entity_Registry::SCOPED_ROUTING_OPTION ] = '0'; WPDO_Hook_Bus::intercept_update( null, 101, 'shared_key', 'legacy', '' ); $this->assertSame( 'legacy', $this->value_in( self::VENDOR_TABLE, 101 ) ); $this->assertNull( $this->value_in( self::LISTING_TABLE, 101 ) ); } }