-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality for filtering job listings on Filled and Featured
- Loading branch information
1 parent
341fb77
commit 7ff287c
Showing
2 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/php/tests/includes/admin/test_class.wp-job-manager-cpt.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
require 'includes/admin/class-wp-job-manager-cpt.php'; | ||
|
||
class WP_Test_WP_Job_Manager_CPT extends WPJM_BaseTest { | ||
public function setUp() { | ||
parent::setUp(); | ||
|
||
// Ensure the hooks are set up. | ||
$this->job_manager_cpt = new WP_Job_Manager_CPT(); | ||
} | ||
|
||
/** | ||
* Ensure that filter_meta adds the correct filters to the query based on | ||
* the URL parameters. | ||
* | ||
* @since 1.31.0 | ||
* @covers WP_Job_Manager_CPT::filter_meta | ||
*/ | ||
public function test_filter_meta() { | ||
global $pagenow; | ||
|
||
// Create some listings. | ||
$listing_notfilled_notfeatured_id = $this->create_listing_with_meta( | ||
array( '_filled' => '0', '_featured' => '0' ) | ||
); | ||
$listing_notfilled_featured_id = $this->create_listing_with_meta( | ||
array( '_filled' => '0', '_featured' => '1' ) | ||
); | ||
$listing_filled_notfeatured_id = $this->create_listing_with_meta( | ||
array( '_filled' => '1', '_featured' => '0' ) | ||
); | ||
$listing_filled_featured_id = $this->create_listing_with_meta( | ||
array( '_filled' => '1', '_featured' => '1' ) | ||
); | ||
|
||
// Simulate viewing the edit.php page. | ||
$pagenow = 'edit.php'; | ||
|
||
// When no filters are given. | ||
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) ); | ||
$this->assertContains( $listing_notfilled_notfeatured_id, $query->posts ); | ||
$this->assertContains( $listing_notfilled_featured_id, $query->posts ); | ||
$this->assertContains( $listing_filled_notfeatured_id, $query->posts ); | ||
$this->assertContains( $listing_filled_featured_id, $query->posts ); | ||
|
||
// Filtering on Filled. | ||
$_GET['job_listing_filled'] = '1'; | ||
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) ); | ||
$this->assertNotContains( $listing_notfilled_notfeatured_id, $query->posts ); | ||
$this->assertNotContains( $listing_notfilled_featured_id, $query->posts ); | ||
$this->assertContains( $listing_filled_notfeatured_id, $query->posts ); | ||
$this->assertContains( $listing_filled_featured_id, $query->posts ); | ||
|
||
// Filtering on Featured. | ||
$_GET['job_listing_filled'] = ''; | ||
$_GET['job_listing_featured'] = '0'; | ||
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) ); | ||
$this->assertContains( $listing_notfilled_notfeatured_id, $query->posts ); | ||
$this->assertNotContains( $listing_notfilled_featured_id, $query->posts ); | ||
$this->assertContains( $listing_filled_notfeatured_id, $query->posts ); | ||
$this->assertNotContains( $listing_filled_featured_id, $query->posts ); | ||
} | ||
|
||
/** | ||
* Ensure that filter_meta adds the correct filters to the query only on | ||
* edit.php page. | ||
* | ||
* @since 1.31.0 | ||
* @covers WP_Job_Manager_CPT::filter_meta | ||
*/ | ||
public function test_filter_meta_only_on_edit() { | ||
global $pagenow; | ||
|
||
// Create some listings. | ||
$listing_id = $this->factory->post->create( | ||
array( 'post_type' => 'job_listing' ) | ||
); | ||
|
||
// Simulate viewing some other page. | ||
$pagenow = 'index.php'; | ||
|
||
// Filter should do nothing | ||
$_GET['job_listing_filled'] = '1'; | ||
$_GET['job_listing_featured'] = '1'; | ||
$query = new WP_Query( array( 'post_type' => 'job_listing', 'fields' => 'ids' ) ); | ||
$this->assertContains( $listing_id, $query->posts ); | ||
} | ||
|
||
/* Helper methods. */ | ||
|
||
private function create_listing_with_meta( $meta ) { | ||
$id = $this->factory->post->create( | ||
array( 'post_type' => 'job_listing' ) | ||
); | ||
|
||
foreach ( $meta as $meta_key => $meta_value ) { | ||
update_post_meta( $id, $meta_key, $meta_value ); | ||
} | ||
|
||
return $id; | ||
} | ||
} |