- Timestamp:
- 01/12/2021 09:30:56 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/api.wordpress.org/public_html/patterns/1.0/tests/test-index.php
r10545 r10572 40 40 41 41 /** 42 * Asserts that an HTTP response is valid and contains a pattern. 43 * 44 * @param Requests_Response $response 45 */ 46 public function assertResponseHasPattern( $response ) { 47 $patterns = json_decode( $response->body ); 48 49 $this->assertSame( 200, $response->status_code ); 50 $this->assertIsString( $patterns[0]->title->rendered ); 51 $this->assertIsInt( $patterns[0]->meta->wpop_viewport_width ); 52 } 53 54 /** 55 * Pluck term IDs from a list of patterns. 56 * 57 * @param object[] $patterns 58 * 59 * @return int[] 60 */ 61 public function get_term_ids( $patterns ) { 62 $term_ids = array(); 63 64 foreach ( $patterns as $pattern ) { 65 $term_ids = array_merge( 66 $term_ids, 67 array_column( $pattern->_embedded->{'wp:term'}[0], 'id' ) 68 ); 69 } 70 71 return array_unique( $term_ids ); 72 } 73 74 /** 42 75 * @covers ::main() 43 76 * 44 77 * @group e2e 45 78 */ 46 public function test_get_all_patterns() : void { 47 $response = $this->send_request( '/?action=get_patterns' ); 48 $body = json_decode( $response->body ); 79 public function test_browse_all_patterns() : void { 80 $response = $this->send_request( '/' ); 81 $patterns = json_decode( $response->body ); 82 $term_ids = $this->get_term_ids( $patterns ); 49 83 50 $this->assertSame( 200, $response->status_code ); 51 $this->assertIsString( $body[0]->title->rendered ); 52 $this->assertIsInt( $body[0]->meta->wpop_viewport_width ); 84 $this->assertResponseHasPattern( $response ); 85 $this->assertGreaterThan( 1, count( $term_ids ) ); 86 } 87 88 /** 89 * @covers ::main() 90 * 91 * @group e2e 92 */ 93 public function test_browse_category() : void { 94 $query_term_id = 2; 95 $response = $this->send_request( '/?pattern-categories=' . $query_term_id ); 96 $patterns = json_decode( $response->body ); 97 $term_ids = $this->get_term_ids( $patterns ); 98 99 $this->assertResponseHasPattern( $response ); 100 $this->assertSame( array( $query_term_id ), $term_ids ); 101 } 102 103 /** 104 * @covers ::main() 105 * 106 * @dataProvider data_search_patterns 107 * 108 * @group e2e 109 * 110 * @param string $search_query 111 */ 112 public function test_search_patterns( $search_term, $match_expected ) : void { 113 $response = $this->send_request( '/?search=' . $search_term ); 114 $patterns = json_decode( $response->body ); 115 116 if ( $match_expected ) { 117 $this->assertResponseHasPattern( $response ); 118 119 $all_patterns_include_query = true; 120 121 foreach ( $patterns as $pattern ) { 122 $match_in_title = stripos( $pattern->title->rendered, $search_term ); 123 $match_in_description = stripos( $pattern->meta->wpop_description, $search_term );; 124 125 if ( ! $match_in_title && ! $match_in_description ) { 126 $all_patterns_include_query = false; 127 break; 128 } 129 } 130 131 $this->assertTrue( $all_patterns_include_query ); 132 133 } else { 134 $this->assertSame( 200, $response->status_code ); 135 $this->assertSame( '[]', $response->body ); 136 } 137 } 138 139 public function data_search_patterns() { 140 return array( 141 'match title' => array( 142 'search_term' => 'side by side', 143 'match_expected' => true, 144 ), 145 146 // todo Enable this once https://github.com/WordPress/pattern-directory/issues/28 is done 147 // 'match description' => array( 148 // 'search_term' => 'bright gradient background', 149 // 'match_expected' => true, 150 // ), 151 152 'no matches' => array( 153 'search_term' => 'Supercalifragilisticexpialidocious', 154 'match_expected' => false, 155 ), 156 ); 53 157 } 54 158 }
Note: See TracChangeset
for help on using the changeset viewer.