createMock( TMDO_Entity_Adapter_Interface::class ); $adapter->method( 'get_entity_id_column' )->willReturn( 'post_id' ); TMDO_Entity_Registry::register_adapter( 'post', $adapter ); } private function fields(): array { return array( array( 'key' => 'x_flag', 'type' => 'integer' ), array( 'key' => 'x_price', 'type' => 'decimal' ), ); } public function test_declared_indexes_are_kept_per_group(): void { TMDO_Entity_Registry::register_group( 'post', 'x_core', $this->fields(), array( 'indexes' => array( 'flag_price' => array( 'x_flag', 'x_price' ) ) ) ); $this->assertSame( array( 'flag_price' => array( 'x_flag', 'x_price' ) ), TMDO_Entity_Registry::get_group_indexes( 'post', 'x_core' ) ); $this->assertSame( array(), TMDO_Entity_Registry::get_group_indexes( 'post', 'other' ) ); } public function test_indexes_on_unknown_columns_are_dropped(): void { TMDO_Entity_Registry::register_group( 'post', 'x_core', $this->fields(), array( 'indexes' => array( 'bad' => array( 'x_flag', 'nope' ), 'ok' => array( 'x_price' ) ) ) ); $this->assertSame( array( 'ok' => array( 'x_price' ) ), TMDO_Entity_Registry::get_group_indexes( 'post', 'x_core' ) ); } /** * A sort index ends with the entity ID, so a flat-first search ordered by a * column with the ID tie-break reads the index in order (2.4 PR-118). */ public function test_an_index_may_end_with_the_entity_id_column(): void { TMDO_Entity_Registry::register_group( 'post', 'x_core', $this->fields(), array( 'indexes' => array( 'price_id' => array( 'x_price', 'post_id' ), 'id_first' => array( 'post_id', 'x_price' ) ) ) ); $this->assertSame( array( 'price_id' => array( 'x_price', 'post_id' ) ), TMDO_Entity_Registry::get_group_indexes( 'post', 'x_core' ), 'the ID only as the last column' ); } public function test_indexes_leave_the_field_definition_and_hash_alone(): void { TMDO_Entity_Registry::register_group( 'post', 'plain', $this->fields() ); TMDO_Entity_Registry::register_group( 'post', 'indexed', $this->fields(), array( 'indexes' => array( 'flag_price' => array( 'x_flag', 'x_price' ) ) ) ); $plain = TMDO_Schema_Manager::calculate_schema_hash( TMDO_Entity_Registry::get_group_fields( 'post', 'plain' ) ); $indexed = TMDO_Schema_Manager::calculate_schema_hash( TMDO_Entity_Registry::get_group_fields( 'post', 'indexed' ) ); $this->assertSame( $plain, $indexed ); } public function test_init_clears_indexes(): void { TMDO_Entity_Registry::register_group( 'post', 'x_core', $this->fields(), array( 'indexes' => array( 'ok' => array( 'x_price' ) ) ) ); TMDO_Entity_Registry::init(); $this->assertSame( array(), TMDO_Entity_Registry::get_group_indexes( 'post', 'x_core' ) ); } }