/** * Test a simple post sync * * @since 0.9 */ public function testPostSync() { $sites = ep_get_sites(); foreach ($sites as $site) { switch_to_blog($site['blog_id']); add_action('ep_sync_on_transition', array($this, 'action_sync_on_transition'), 10, 0); $post_id = epa_create_and_sync_post(); ep_refresh_index(); // $this->assertTrue( ! empty( $this->fired_actions['ep_sync_on_transition'] ) ); $post = ep_get_post($post_id); $this->assertTrue(!empty($post)); $this->fired_actions = array(); restore_current_blog(); } }
function ep_create_date_query_posts() { $sites = ep_get_sites(); $beginning_tz = date_default_timezone_get(); date_default_timezone_set('America/Los_Angeles'); foreach ($sites as $site) { switch_to_blog($site['blog_id']); $post_date = strtotime("January 6th, 2012 11:59PM"); for ($i = 0; $i <= 10; ++$i) { ep_create_and_sync_post(array('post_title' => 'post_title' . $site['blog_id'], 'post_content' => 'findme', 'post_date' => date("Y-m-d H:i:s", strtotime("-{$i} days", strtotime("-{$i} hours", $post_date))), 'post_date_gmt' => gmdate("Y-m-d H:i:s", strtotime("-{$i} days", strtotime("-{$i} hours", $post_date))))); ep_refresh_index(); } restore_current_blog(); } date_default_timezone_set($beginning_tz); }
public function testPostParentQuery() { $parent_post = ep_create_and_sync_post(array('post_content' => 'findme test 1')); ep_create_and_sync_post(array('post_content' => 'findme test 2', 'post_parent' => $parent_post)); ep_create_and_sync_post(array('post_content' => 'findme test 3')); ep_refresh_index(); $args = array('s' => 'findme', 'post_parent' => $parent_post); $query = new WP_Query($args); $this->assertEquals(1, $query->post_count); $this->assertEquals(1, $query->found_posts); }
/** * Test what happens when no post types are available to be searched * * @since 1.3 */ public function testNoAvailablePostTypesToSearch() { $post_ids = array(); $post_ids[0] = ep_create_and_sync_post(); $post_ids[1] = ep_create_and_sync_post(); $post_ids[2] = ep_create_and_sync_post(array('post_content' => 'findme')); $post_ids[3] = ep_create_and_sync_post(); $post_ids[4] = ep_create_and_sync_post(array('post_content' => 'findme')); $GLOBALS['wp_post_types']; $backup_post_types = $GLOBALS['wp_post_types']; // Set all post types to be excluded from search foreach ($GLOBALS['wp_post_types'] as $post_type) { $post_type->exclude_from_search = true; } ep_refresh_index(); $args = array('s' => 'findme'); add_action('ep_wp_query_search', array($this, 'action_wp_query_search'), 10, 0); $query = new WP_Query($args); $this->assertTrue(empty($this->fired_actions['ep_wp_query_search'])); $this->assertEquals($query->post_count, 0); $this->assertEquals($query->found_posts, 0); wp_reset_postdata(); // Reset the main $wp_post_types item $GLOBALS['wp_post_types'] = $backup_post_types; }
/** * Test case for when index is deleted, request for Elasticsearch should fall back to WP Query */ public function testSearchIndexDeleted() { global $wpdb; ep_activate_module('search'); EP_Modules::factory()->setup_modules(); $post_ids = array(); ep_create_and_sync_post(); ep_create_and_sync_post(); ep_create_and_sync_post(array('post_content' => 'findme')); ep_refresh_index(); add_action('ep_wp_query_search', array($this, 'action_wp_query_search'), 10, 0); $args = array('s' => 'findme'); $query = new WP_Query($args); $this->assertTrue("SELECT * FROM {$wpdb->posts} WHERE 1=0" == $query->request); ep_delete_index(); $query = new WP_Query($args); $this->assertTrue("SELECT * FROM {$wpdb->posts} WHERE 1=0" != $query->request); }
/** * Test date time meta queries * * @since 1.7 */ public function testMetaValueTypeQueryDatetime() { ep_create_and_sync_post(array('post_content' => 'the post content findme')); ep_create_and_sync_post(array('post_content' => 'post content findme'), array('test_key' => '5:00am 1/2/12')); ep_refresh_index(); $args = array('s' => 'findme', 'meta_query' => array(array('key' => 'test_key', 'value' => '2013-03-02 06:00:15', 'compare' => '<', 'type' => 'datetime'))); $query = new WP_Query($args); $this->assertEquals(1, $query->post_count); $args = array('s' => 'findme', 'meta_query' => array(array('key' => 'test_key', 'value' => '2012-01-02 05:00:00', 'compare' => '=', 'type' => 'datetime'))); $query = new WP_Query($args); $this->assertEquals(1, $query->post_count); $args = array('s' => 'findme', 'meta_query' => array(array('key' => 'test_key', 'value' => '2011-01-02 07:30:00', 'compare' => '>', 'type' => 'datetime'))); $query = new WP_Query($args); $this->assertEquals(1, $query->post_count); }
public function testPostInvalidDateTime() { add_filter('ep_indexable_post_status', array($this, 'mock_indexable_post_status'), 10, 1); $post_id = ep_create_and_sync_post(array('post_status' => 'draft')); ep_refresh_index(); ep_sync_post($post_id); wp_cache_flush(); $wp_post = get_post($post_id); $post = ep_get_post($post_id); $invalid_datetime = "0000-00-00 00:00:00"; if ($wp_post->post_date_gmt == $invalid_datetime) { $this->assertNull($post['post_date_gmt']); } if ($wp_post->post_modified_gmt == $invalid_datetime) { $this->assertNull($post['post_modified_gmt']); } $this->assertNotNull($post); remove_filter('ep_indexable_post_status', array($this, 'mock_indexable_post_status'), 10); }
/** * Test post object data * * @since 1.4 */ public function testPostObject() { $sites = ep_get_sites(); $user_id = $this->factory->user->create(array('user_login' => 'john', 'role' => 'administrator')); foreach ($sites as $site) { switch_to_blog($site['blog_id']); ep_create_and_sync_post(array('post_title' => 'findme', 'post_author' => $user_id, 'post_excerpt' => 'find', 'menu_order' => $site['blog_id'])); ep_create_and_sync_post(array('post_title' => 'findme', 'post_author' => $user_id, 'post_excerpt' => 'find', 'menu_order' => $site['blog_id'])); ep_refresh_index(); restore_current_blog(); } $args = array('s' => 'findme', 'sites' => 'all', 'posts_per_page' => 10); $query = new WP_Query($args); $this->assertEquals(6, $query->post_count); $this->assertEquals(6, $query->found_posts); while ($query->have_posts()) { $query->the_post(); global $post; $this->assertEquals($user_id, $post->post_author); $this->assertEquals('find', $post->post_excerpt); $this->assertEquals($post->site_id, $post->menu_order); } wp_reset_postdata(); }
/** * Test search integration is on for shop orders * * @since 2.1 * @group woocommerce */ public function testSearchOnShopOrderAdmin() { ep_activate_module('woocommerce'); EP_Modules::factory()->setup_modules(); ep_create_and_sync_post(array('post_content' => 'findme', 'post_type' => 'shop_order')); ep_refresh_index(); add_action('ep_wp_query_search', array($this, 'action_wp_query_search'), 10, 0); $args = array('s' => 'findme', 'post_type' => 'shop_order'); $query = new WP_Query($args); $this->assertTrue(!empty($this->fired_actions['ep_wp_query_search'])); $this->assertEquals(1, $query->post_count); $this->assertEquals(1, $query->found_posts); }
/** * Check posts filter by category in dashboard */ public function testAdminCategories() { set_current_screen('edit.php'); ep_activate_module('admin'); EP_Modules::factory()->setup_modules(); $cat1 = wp_create_category('category one'); $cat2 = wp_create_category('category two'); ep_create_and_sync_post(array('post_category' => array($cat1))); ep_create_and_sync_post(array('post_category' => array($cat2))); ep_create_and_sync_post(array('post_category' => array($cat1))); ep_refresh_index(); add_action('ep_wp_query_search', array($this, 'action_wp_query_search'), 10, 0); $query = new WP_Query(); global $wp_the_query; $wp_the_query = $query; $args = array('category_name' => 'category one'); $query->query($args); $this->assertTrue(!empty($this->fired_actions['ep_wp_query_search'])); $this->assertEquals(2, $query->post_count); $this->assertEquals(2, $query->found_posts); }
/** * Test for related post args filter */ public function testFindRelatedPostFilter() { $post_id = ep_create_and_sync_post(array('post_content' => 'findme test 1')); ep_create_and_sync_post(array('post_content' => 'findme test 2')); ep_create_and_sync_post(array('post_content' => 'findme test 3', 'post_type' => 'page')); ep_refresh_index(); ep_activate_module('related_posts'); EP_Modules::factory()->setup_modules(); add_filter('ep_find_related_args', array($this, 'find_related_posts_filter'), 10, 1); $related = ep_find_related($post_id); $this->assertEquals(2, sizeof($related)); remove_filter('ep_find_related_args', array($this, 'find_related_posts_filter')); $related = ep_find_related($post_id); $this->assertEquals(3, sizeof($related)); }
/** * Test if $post object values exist after receiving odd values from the 'ep_search_post_return_args' filter. * @group 306 * @link https://github.com/10up/ElasticPress/issues/306 */ public function testPostReturnArgs() { add_filter('ep_search_post_return_args', array($this, 'ep_search_post_return_args_filter')); ep_create_and_sync_post(array('post_content' => 'findme')); ep_refresh_index(); $args = array('s' => 'findme'); $query = new WP_Query($args); remove_filter('ep_search_post_return_args', array($this, 'ep_search_post_return_args_filter')); }
/** * Test filter for skipping query integration * * @since 1.2 */ public function testQueryIntegrationSkip() { $main_post_id = $this->factory->post->create(); query_posts(array('p' => $main_post_id)); $GLOBALS['wp_the_query'] = $GLOBALS['wp_query']; $sites = ep_get_sites(); $i = 0; foreach ($sites as $site) { switch_to_blog($site['blog_id']); ep_create_and_sync_post(array('post_title' => 'findme')); ep_create_and_sync_post(array('post_title' => 'findme')); if ($i > 0) { ep_create_and_sync_post(array('post_title' => 'notfirstblog')); } elseif ($i === 0) { ep_create_and_sync_post(array('post_title' => 'firstblog')); } ep_refresh_index(); restore_current_blog(); $i++; } add_filter('ep_skip_query_integration', '__return_true'); $args = array('s' => 'notfirstblog', 'sites' => 'all'); $query = new WP_Query($args); $this->assertTrue(empty($query->posts)); }