WordPress.org

Making WordPress.org

Changeset 11187


Ignore:
Timestamp:
08/18/2021 03:23:52 PM (3 months ago)
Author:
iandunn
Message:

Patterns: Add tests for boosting title & primary locale.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/patterns/1.0/tests/test-index.php

    r11186 r11187  
    200200            $patterns    = json_decode( $response->body );
    201201            $pattern_ids = array_column( $patterns, 'id' );
     202
    202203            $this->assertAllPatternsMatchSearchTerm( $patterns, $search_term );
    203204
     
    229230                'expected_post_ids' => array( 185 ),
    230231            ),
    231 
    232232
    233233            "don't match post_content" => array(
     
    297297     * @group e2e
    298298     */
     299    public function test_search_title_match_boosted_above_description_match() : void {
     300        $search_term = 'image';
     301        $locale      = 'en_US';
     302
     303        $response = send_request( "/patterns/1.0/?search=$search_term&pattern-keywords=11&locale=$locale" );
     304        $this->assertResponseHasPattern( $response );
     305
     306        $patterns = json_decode( $response->body );
     307
     308        $this->assertAllPatternsMatchSearchTerm( $patterns, $search_term );
     309
     310        $actualOrder = array_column( $patterns, 'id' );
     311
     312        // Intentionally _not_ using `uasort()`, in order to catch different key orders.
     313        $expectedPatterns = json_decode( json_encode( $patterns ) );
     314
     315        usort( $expectedPatterns, function( $a, $b ) use ( $search_term ) {
     316            $adjustment       = 0;
     317            $found_in_title_a = false === stripos( $search_term, $a->title->rendered );
     318            $found_in_title_b = false === stripos( $search_term, $b->title->rendered );
     319
     320            if ( $found_in_title_a && ! $found_in_title_b ) {
     321                $adjustment = -1;
     322            } elseif ( ! $found_in_title_a && $found_in_title_b ) {
     323                $adjustment = 1;
     324            }
     325
     326            return $adjustment;
     327        } );
     328
     329        $expectedOrder = array_column( $expectedPatterns, 'id' );
     330
     331        $this->assertSame( $expectedOrder, $actualOrder );
     332    }
     333
     334    /**
     335     * @covers ::main()
     336     *
     337     * @group e2e
     338     */
     339    public function test_search_locale_sort() : void {
     340        $search_term = 'offset';
     341        $locale      = 'nl_NL';
     342
     343        $response = send_request( "/patterns/1.0/?search=$search_term&pattern-keywords=11&locale=$locale" );
     344        $this->assertResponseHasPattern( $response );
     345
     346        $patterns = json_decode( $response->body );
     347
     348        $this->assertAllPatternsMatchSearchTerm( $patterns, $search_term );
     349
     350        $this->markTestIncomplete(); // todo the following code works, but `WordPressdotorg\Pattern_Directory\Search\modify_es_query_args` isn't boosting the primary locale yet
     351
     352        $actualOrder = array_column( array_column( $patterns, 'meta' ), 'wpop_locale'  );
     353
     354        // Intentionally _not_ using `uasort()`, in order to catch different key orders.
     355        $expectedOrder = json_decode( json_encode( $actualOrder ) );
     356
     357        usort( $expectedOrder, function( $a, $b ) {
     358            $adjustment = 0;
     359
     360            if ( $a !== 'en_US' && $b === 'en_US' ) {
     361                $adjustment = -1;
     362            } elseif ( $a === 'en_US' && $b !== 'en_US' ) {
     363                $adjustment = 1;
     364            }
     365
     366            return $adjustment;
     367        } );
     368
     369        $this->assertSame( $expectedOrder, $actualOrder );
     370    }
     371
     372    /**
     373     * @covers ::main()
     374     *
     375     * @group e2e
     376     */
    299377    public function test_browse_all_categories() : void {
    300378        $response = send_request( '/patterns/1.0/?categories&pattern-keywords=11&locale=en_US' );
Note: See TracChangeset for help on using the changeset viewer.