Repository navigation
Preserve the active entries sort in CSV exports #3702
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
9ad0586
db30375
5d4929d
7459ae0
3c79cff
38b1a98
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,240 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @group csv | ||
| * | ||
| * @covers FrmCSVExportHelper | ||
| * @covers FrmEntriesListHelper | ||
| * @covers FrmEntry::get_sorted_ids | ||
| */ | ||
| class test_FrmCSVExportSort extends FrmUnitTest { | ||
|
|
||
| public function test_descending_export_spans_multiple_batches() { | ||
| $form = $this->factory->form->create_and_get(); | ||
| $ids = $this->factory->entry->create_many( 25, $this->factory->field->generate_entry_array( $form ) ); | ||
| $sort = FrmEntriesListHelper::get_sort_query( 'id', 'DESC' ); | ||
| $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); | ||
|
|
||
| $this->assertSame( array_reverse( $ids ), $this->exported_ids( $form, $sorted_ids ), 'Descending order should survive every CSV batch.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| public function test_numeric_field_export_uses_numeric_order() { | ||
| $form = $this->factory->form->create_and_get(); | ||
| $field_id = $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'type' => 'number', | ||
| ) | ||
| ); | ||
| $ids = array(); | ||
|
|
||
| foreach ( array( '10', '2', '30' ) as $value ) { | ||
| $ids[] = $this->factory->entry->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'item_meta' => array( $field_id => $value ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $sort = FrmEntriesListHelper::get_sort_query( 'meta_' . $field_id, 'ASC' ); | ||
| $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); | ||
| $this->assertSame( array( $ids[1], $ids[0], $ids[2] ), $this->exported_ids( $form, $sorted_ids ), 'Numeric sorting should place 2 before 10.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| public function test_text_field_export_uses_field_order() { | ||
| $form = $this->factory->form->create_and_get(); | ||
| $field_id = $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'type' => 'text', | ||
| ) | ||
| ); | ||
| $ids = array(); | ||
|
|
||
| foreach ( array( 'Pear', 'Apple', 'Orange' ) as $value ) { | ||
| $ids[] = $this->factory->entry->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'item_meta' => array( $field_id => $value ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $sort = FrmEntriesListHelper::get_sort_query( 'meta_' . $field_id, 'DESC' ); | ||
| $sorted_ids = FrmEntry::get_sorted_ids( array( 'form_id' => $form->id ), $sort ); | ||
| $this->assertSame( array( $ids[0], $ids[2], $ids[1] ), $this->exported_ids( $form, $sorted_ids ), 'Text sorting should follow the selected direction.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| public function test_export_preserves_the_supplied_id_sequence() { | ||
| $form = $this->factory->form->create_and_get(); | ||
| $ids = $this->factory->entry->create_many( 25, $this->factory->field->generate_entry_array( $form ) ); | ||
| $ids = array_merge( array_slice( $ids, 12 ), array_slice( $ids, 0, 12 ) ); | ||
|
|
||
| $this->assertSame( $ids, $this->exported_ids( $form, $ids ), 'CSV batches should preserve arbitrary caller order.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| /** | ||
| * @covers FrmXMLController::csv | ||
| * | ||
| * @runInSeparateProcess | ||
| * | ||
| * @preserveGlobalState disabled | ||
|
Comment on lines
+80
to
+82
Contributor
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. Add process-isolation attributes for PHPUnit 12. The declared This test sends CSV download headers through Suggested fix
|
||
| */ | ||
| public function test_controller_reads_the_requested_sort() { | ||
| $this->set_current_user_to_1(); | ||
| $form = $this->factory->form->create_and_get(); | ||
| $field_id = $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'type' => 'number', | ||
| ) | ||
| ); | ||
| $ids = array(); | ||
|
|
||
| foreach ( array( '10', '2', '30' ) as $value ) { | ||
| $ids[] = $this->factory->entry->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'item_meta' => array( $field_id => $value ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $exported = $this->exported_ids_from_controller( | ||
| array( | ||
| 'form' => $form->id, | ||
| 'orderby' => 'meta_' . $field_id, | ||
| 'order' => 'DESC', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( array( $ids[2], $ids[0], $ids[1] ), $exported, 'The controller should read both sort parameters from the URL.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| /** | ||
| * @covers FrmXMLController::csv | ||
| * | ||
| * @runInSeparateProcess | ||
| * | ||
| * @preserveGlobalState disabled | ||
| */ | ||
| public function test_controller_applies_items_table_conditions_with_the_sort() { | ||
| $this->set_current_user_to_1(); | ||
| $form = $this->factory->form->create_and_get(); | ||
| $field_id = $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'type' => 'number', | ||
| ) | ||
| ); | ||
| $ids = array(); | ||
|
|
||
| foreach ( array( '10', '2', '30', '5' ) as $index => $value ) { | ||
| $ids[] = $this->factory->entry->create( | ||
| array( | ||
| 'form_id' => $form->id, | ||
| 'item_meta' => array( $field_id => $value ), | ||
| 'is_draft' => 2 === $index ? 1 : 0, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $excluded = $ids[3]; | ||
| $filter = static function ( $query ) use ( $excluded ) { | ||
| $query['it.is_draft'] = 0; | ||
| $query['it.id !'] = $excluded; | ||
| return $query; | ||
| }; | ||
| add_filter( 'frm_csv_where', $filter ); | ||
|
|
||
| try { | ||
| $exported = $this->exported_ids_from_controller( | ||
| array( | ||
| 'form' => $form->id, | ||
| 'orderby' => 'meta_' . $field_id, | ||
| 'order' => 'DESC', | ||
| ) | ||
| ); | ||
| } finally { | ||
| remove_filter( 'frm_csv_where', $filter ); | ||
| } | ||
|
|
||
| $this->assertSame( array( $ids[0], $ids[1] ), $exported, 'Conditions on the aliased items table should filter the sorted export.' ); | ||
|
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.
|
||
| } | ||
|
|
||
| /** | ||
| * Run the CSV controller with the given URL parameters and return the exported entry IDs in order. | ||
| * | ||
| * @param array $params Query string parameters. | ||
| * | ||
| * @return array | ||
| */ | ||
| private function exported_ids_from_controller( $params ) { | ||
| $get = $_GET; | ||
| $request = $_REQUEST; | ||
| $_GET = $params; | ||
| $_REQUEST = $params; | ||
| $handler = static function () { | ||
| return static function () { | ||
| throw new RuntimeException( 'CSV complete.' ); | ||
| }; | ||
| }; | ||
| add_filter( 'wp_die_handler', $handler ); | ||
| ob_start(); | ||
|
|
||
| try { | ||
| FrmXMLController::csv(); | ||
| } catch ( RuntimeException $exception ) { | ||
| $this->assertSame( 'CSV complete.', $exception->getMessage(), 'The CSV controller should finish through wp_die.' ); | ||
|
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.
|
||
| } finally { | ||
| $csv = ob_get_clean(); | ||
| remove_filter( 'wp_die_handler', $handler ); | ||
| $_GET = $get; | ||
| $_REQUEST = $request; | ||
| } | ||
|
|
||
| $lines = preg_split( '/\r\n|\n|\r/', trim( $csv ) ); | ||
| $headings = str_getcsv( array_shift( $lines ), ',', '"', '' ); | ||
| $id_column = array_search( 'ID', $headings, true ); | ||
| $exported = array(); | ||
|
|
||
| foreach ( $lines as $line ) { | ||
| $row = str_getcsv( $line, ',', '"', '' ); | ||
| $exported[] = (int) $row[ $id_column ]; | ||
| } | ||
|
|
||
| return $exported; | ||
| } | ||
|
|
||
| /** | ||
| * @param object $form Form being exported. | ||
| * @param array $ids Ordered entry IDs. | ||
| * | ||
| * @return array | ||
| */ | ||
| private function exported_ids( $form, $ids ) { | ||
| $path = FrmCSVExportHelper::generate_csv( | ||
| array( | ||
| 'mode' => 'file', | ||
| 'form' => $form, | ||
| 'entry_ids' => $ids, | ||
| 'form_cols' => FrmField::get_all_for_form( $form->id, '', 'include' ), | ||
| ) | ||
| ); | ||
| WP_Filesystem(); | ||
| global $wp_filesystem; | ||
| $lines = preg_split( '/\r\n|\n|\r/', trim( $wp_filesystem->get_contents( $path ) ) ); | ||
| $headings = str_getcsv( array_shift( $lines ), ',', '"', '' ); | ||
| $id_column = array_search( 'ID', $headings, true ); | ||
| $rows = array(); | ||
|
|
||
| foreach ( $lines as $line ) { | ||
| $row = str_getcsv( $line, ',', '"', '' ); | ||
| $rows[] = (int) $row[ $id_column ]; | ||
| } | ||
|
|
||
| wp_delete_file( $path ); | ||
| return $rows; | ||
| } | ||
| } | ||
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.
Keep the original table name valid for
frm_csv_whereconditions.get_sorted_ids()queriesfrm_items it. After aliasing, the original table name cannot qualify columns. Afrm_csv_wherecallback that returns a condition such aswp_frm_items.created_at >can fail with an unknown-column error. The export then cannot return its filtered entries.FrmDbkeeps the key as written, so the qualifier reaches the query unchanged.The filter runs at
classes/controllers/FrmXMLController.php:741, beforeget_sorted_ids()runs at line 746. No callback in this checkout uses a qualified key. Formidable Pro and other integrations are not in this checkout, so their callbacks cannot be checked here. Check those callbacks for qualifiers that use the original table name. If they exist, either keep the table unaliased in this query or update those callbacks to useit. A safe edit depends on whether the alias is required by the field-sort expression, which this checkout does not show, so no code change is proposed here.