Repository navigation
Show IDs in page and View dropdown labels #3684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
89757f3
3acba6b
84cdf0f
d7d804b
0984677
79a98a2
339e325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <?php | ||
| /** Verify consistent page and View choices without changing stored IDs. */ | ||
| class test_FrmPostDropdownLabels extends FrmUnitTest { | ||
| /** @return void */ | ||
| public function test_titles_and_ids() { | ||
| foreach ( array( '', ' ', 'Saved Listings', '0', 'A & B <script>alert(1)</script>' ) as $title ) { | ||
| $post = (object) array( | ||
| 'ID' => 123, | ||
| 'post_title' => $title, | ||
| ); | ||
| $expected = '' === trim( $title ) ? FrmFormsHelper::get_no_title_text() : $title; | ||
| $this->assertSame( $expected . ' (ID: 123)', FrmAppHelper::get_post_dropdown_label( $post ) ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( $title, $post->post_title ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| /** @return void */ | ||
| public function test_truncated_title_keeps_id() { | ||
| $post = (object) array( | ||
| 'ID' => 456, | ||
| 'post_title' => str_repeat( 'Long title ', 20 ), | ||
| ); | ||
| $label = FrmAppHelper::get_post_dropdown_label( $post, 30 ); | ||
| $this->assertStringContainsString( '(ID: 456)', $label ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertLessThan( strlen( $post->post_title ), strlen( $label ) ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** @return void */ | ||
| public function test_page_search_matches_prefilled_label_and_id() { | ||
| $id = self::factory()->post->create( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array( | ||
| 'post_type' => 'page', | ||
| 'post_status' => 'publish', | ||
| 'post_title' => 'Saved Listings', | ||
| ) | ||
| ); | ||
|
|
||
| $terms = array( | ||
| 'Saved Listings', | ||
| 'Saved Listings (ID: ' . $id . ')', | ||
| 'Saved Listings (ID: 6', | ||
| 'Saved Listings (ID: ', | ||
| 'Saved Listings (ID', | ||
| 'Saved Listings (I', | ||
| 'Saved Listings (', | ||
| 'Saved', | ||
| (string) $id, | ||
| ); | ||
|
|
||
| foreach ( $terms as $term ) { | ||
| $values = wp_list_pluck( FrmSettingsController::get_page_search_results( $term, 'page' ), 'value' ); | ||
| $this->assertContains( (string) $id, array_map( 'strval', $values ), 'Term: ' . $term ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| $results = FrmSettingsController::get_page_search_results( 'Saved Listings', 'page' ); | ||
| $this->assertSame( 'Saved Listings (ID: ' . $id . ')', $results[0]['label'] ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( array(), FrmSettingsController::get_page_search_results( 'No such page', 'page' ) ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** @return void */ | ||
| public function test_autocomplete_selected_value_keeps_private_prefix() { | ||
| $id = self::factory()->post->create( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array( | ||
| 'post_type' => 'page', | ||
| 'post_status' => 'private', | ||
| 'post_title' => 'Secret', | ||
| ) | ||
| ); | ||
| self::factory()->post->create_many( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 51, | ||
| array( | ||
| 'post_type' => 'page', | ||
| 'post_status' => 'publish', | ||
| ) | ||
| ); | ||
| ob_start(); | ||
| FrmAppHelper::maybe_autocomplete_pages_options( | ||
| array( | ||
| 'field_name' => 'destination', | ||
| 'page_id' => $id, | ||
| ) | ||
| ); | ||
| $html = ob_get_clean(); | ||
| $this->assertStringContainsString( 'value="Private: Secret (ID: ' . $id . ')"', $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** @return void */ | ||
| public function test_shared_page_dropdown_uses_no_title() { | ||
| $id = self::factory()->post->create( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array( | ||
| 'post_type' => 'page', | ||
| 'post_status' => 'publish', | ||
| 'post_title' => '', | ||
| ) | ||
| ); | ||
| ob_start(); | ||
| FrmAppHelper::wp_pages_dropdown( | ||
| array( | ||
| 'field_name' => 'destination', | ||
| 'page_id' => $id, | ||
| ) | ||
| ); | ||
| $html = ob_get_clean(); | ||
| $this->assertStringContainsString( '(no title) (ID: ' . $id . ')', $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertStringContainsString( 'value="' . $id . '"', $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** @return void */ | ||
| public function test_gated_content_autocomplete_keeps_values() { | ||
| $source = json_decode( | ||
| FrmGatedContentAction::get_posts_autocomplete_source( | ||
| array( | ||
| (object) array( | ||
| 'ID' => 123, | ||
| 'post_title' => '', | ||
| ), | ||
| ) | ||
| ), | ||
| true | ||
| ); | ||
| $this->assertSame( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array( | ||
| array( | ||
| 'value' => '123', | ||
| 'label' => '(no title) (ID: 123)', | ||
| ), | ||
| ), | ||
| $source | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix. The label now carries
(ID: n), but the query above it still only matchespost_title. I ran the sameFrmDb::get_results( $wpdb->posts, array( ..., 'post_title LIKE' => $term ), ... )against a published page titledSaved Listings(ID 24):Saved Listings→ 1 rowSaved Listings (ID: 24)→ 0 rows24→ 0 rowsSo the pre-filled value (
FrmAppHelper::maybe_autocomplete_pages_options()) cannot be refined without losing every match, and the ID that this PR is adding cannot be used to find the page. Fix: strip a trailing(ID: n)from the term and also match an all-digit term againstID. Sketch:and extend
$wherewith anIDOR clause whenctype_digit( $term ). Add a test that callspage_search()(or the query) with a prefilled label and with a numeric ID.