function setUp() { parent::setUp(); sp_index_flush_data(); $this->demo_user = array('user_login' => 'author1', 'user_nicename' => 'author-nicename', 'user_pass' => rand_str(), 'role' => 'author', 'display_name' => 'Michael Scott'); $this->demo_user_id = $this->factory->user->create($this->demo_user); $this->demo_term = array('taxonomy' => 'category', 'name' => 'cat-a', 'slug' => 'cat-a'); $this->demo_term_id = $this->factory->term->create($this->demo_term); $post_date = ''; $this->demo_dates = array('post_date' => array('date' => '2013-02-28 01:23:45'), 'post_date_gmt' => array('date' => '2013-02-28 05:23:45'), 'post_modified' => array('date' => '2013-02-28 01:23:45'), 'post_modified_gmt' => array('date' => '2013-02-28 05:23:45')); foreach ($this->demo_dates as &$date) { $ts = strtotime($date['date']); $date = array('date' => strval($date['date']), 'year' => intval(date('Y', $ts)), 'month' => intval(date('m', $ts)), 'day' => intval(date('d', $ts)), 'hour' => intval(date('H', $ts)), 'minute' => intval(date('i', $ts)), 'second' => intval(date('s', $ts)), 'week' => intval(date('W', $ts)), 'day_of_week' => intval(date('N', $ts)), 'day_of_year' => intval(date('z', $ts)), 'seconds_from_day' => intval(mktime(date('H', $ts), date('i', $ts), date('s', $ts), 1, 1, 1970)), 'seconds_from_hour' => intval(mktime(0, date('i', $ts), date('s', $ts), 1, 1, 1970))); } $this->demo_post = array('post_author' => $this->demo_user_id, 'post_date' => $this->demo_dates['post_date']['date'], 'post_date_gmt' => $this->demo_dates['post_date_gmt']['date'], 'post_content' => 'Welcome to <a href="http://wp.dev/">Local WordPress Dev Sites</a>. This is your first post. Edit or delete it, then start blogging!', 'post_title' => 'Hello world!', 'post_excerpt' => 'Lorem ipsum dolor sit amet', 'post_status' => 'publish', 'post_password' => 'foobar', 'post_name' => 'hello-world', 'post_parent' => 123, 'menu_order' => 456, 'post_type' => 'post', 'post_mime_type' => 'image/jpeg', 'post_category' => array($this->demo_term_id)); $this->demo_post_id = $this->factory->post->create($this->demo_post); add_post_meta($this->demo_post_id, 'test_string', 'foo'); add_post_meta($this->demo_post_id, 'test_long', '123'); add_post_meta($this->demo_post_id, 'test_double', '123.456'); add_post_meta($this->demo_post_id, 'test_boolean_true', 'true'); add_post_meta($this->demo_post_id, 'test_boolean_false', 'false'); add_post_meta($this->demo_post_id, 'test_date', '2012-03-14 03:14:15'); SP_Sync_Manager()->sync_post($this->demo_post_id); // Force refresh the index so the data is available immediately SP_API()->post('_refresh'); }
function sp_manually_load_plugin() { // If your ES server is not at localhost:9200, you need to set $_ENV['SEARCHPRESS_HOST']. $host = getenv('SEARCHPRESS_HOST'); if (empty($host)) { $host = 'http://localhost:9200'; } update_option('sp_settings', array('host' => $host, 'must_init' => false, 'active' => true, 'last_beat' => false)); require dirname(__FILE__) . '/../searchpress.php'; SP_API()->index = 'searchpress-tests'; // Make sure ES is running and responding $tries = 5; $sleep = 3; do { $response = wp_remote_get('http://localhost:9200/'); if ('200' == wp_remote_retrieve_response_code($response)) { // Looks good! break; } else { printf("\nInvalid response from ES (%s), sleeping %d seconds and trying again...\n", wp_remote_retrieve_response_code($response), $sleep); sleep($sleep); } } while (--$tries); // If we didn't end with a 200 status code, exit sp_tests_verify_response_code($response); sp_index_flush_data(); }
/** * Flush the current index. !!Warning!! This empties your elasticsearch index for the entire site. * */ public function flush() { WP_CLI::line("Flushing current index..."); $result = SP_Config()->flush(); if ('200' == SP_API()->last_request['response_code'] || '404' == SP_API()->last_request['response_code']) { WP_CLI::success("Successfully flushed Post index\n"); } else { print_r(SP_API()->last_request); print_r($result); WP_CLI::error("Could not flush existing data!"); } }
function setUp() { parent::setUp(); sp_index_flush_data(); $this->factory->post->create(array('post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00')); $this->factory->post->create(array('post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00')); $this->factory->post->create(array('post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00')); $this->factory->post->create(array('post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00')); $this->factory->post->create(array('post_title' => 'no-comments', 'post_date' => '2009-10-02 00:00:00')); $this->factory->post->create(array('post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00')); $this->factory->post->create(array('post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00')); $this->factory->post->create(array('post_title' => 'embedded-video', 'post_date' => '2010-01-01 00:00:00')); $this->factory->post->create(array('post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00')); $this->factory->post->create(array('post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00')); // Force refresh the index so the data is available immediately SP_API()->post('_refresh'); }
function setUp() { parent::setUp(); sp_index_flush_data(); $cat_a = $this->factory->term->create(array('taxonomy' => 'category', 'name' => 'cat-a')); $cat_b = $this->factory->term->create(array('taxonomy' => 'category', 'name' => 'cat-b')); $cat_c = $this->factory->term->create(array('taxonomy' => 'category', 'name' => 'cat-c')); $this->factory->post->create(array('post_title' => 'cats-a-b-c', 'post_date' => '2008-12-01 00:00:00', 'post_category' => array($cat_a, $cat_b, $cat_c))); $this->factory->post->create(array('post_title' => 'cats-a-and-b', 'post_date' => '2009-01-01 00:00:00', 'post_category' => array($cat_a, $cat_b))); $this->factory->post->create(array('post_title' => 'cats-b-and-c', 'post_date' => '2009-02-01 00:00:00', 'post_category' => array($cat_b, $cat_c))); $this->factory->post->create(array('post_title' => 'cats-a-and-c', 'post_date' => '2009-03-01 00:00:00', 'post_category' => array($cat_a, $cat_c))); $this->factory->post->create(array('post_title' => 'cat-a', 'post_date' => '2009-04-01 00:00:00', 'post_category' => array($cat_a))); $this->factory->post->create(array('post_title' => 'cat-b', 'post_date' => '2009-05-01 00:00:00', 'post_category' => array($cat_b))); $this->factory->post->create(array('post_title' => 'cat-c', 'post_date' => '2009-06-01 00:00:00', 'post_category' => array($cat_c))); $this->factory->post->create(array('post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00', 'post_category' => array($cat_a))); $this->factory->post->create(array('post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00', 'post_category' => array($cat_a))); $this->factory->post->create(array('post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00', 'post_category' => array($cat_b))); $this->factory->post->create(array('post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00')); $this->factory->post->create(array('post_title' => 'no-comments', 'post_date' => '2009-10-02 00:00:00')); $this->factory->post->create(array('post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00')); $this->factory->post->create(array('post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00')); $this->factory->post->create(array('post_title' => 'embedded-video', 'post_date' => '2010-01-01 00:00:00')); $this->factory->post->create(array('post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00')); $this->factory->post->create(array('post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tags-a-b-c', 'tags_input' => array('tag-a', 'tag-b', 'tag-c'), 'post_date' => '2010-04-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tag-a', 'tags_input' => array('tag-a'), 'post_date' => '2010-05-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tag-a-2', 'tags_input' => array('tag-a'), 'post_date' => '2010-05-01 00:00:01')); $this->factory->post->create(array('post_title' => 'tag-a-3', 'tags_input' => array('tag-a'), 'post_date' => '2010-05-01 00:00:02')); $this->factory->post->create(array('post_title' => 'tag-b', 'tags_input' => array('tag-b'), 'post_date' => '2010-06-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tag-b-2', 'tags_input' => array('tag-b'), 'post_date' => '2010-06-01 00:00:01')); $this->factory->post->create(array('post_title' => 'tag-c', 'tags_input' => array('tag-c'), 'post_date' => '2010-07-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tags-a-and-b', 'tags_input' => array('tag-a', 'tag-b'), 'post_date' => '2010-08-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tags-b-and-c', 'tags_input' => array('tag-b', 'tag-c'), 'post_date' => '2010-09-01 00:00:00')); $this->factory->post->create(array('post_title' => 'tags-a-and-c', 'tags_input' => array('tag-a', 'tag-c'), 'post_date' => '2010-10-01 00:00:00')); $this->parent_one = $this->factory->post->create(array('post_title' => 'parent-one', 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:01')); $this->parent_two = $this->factory->post->create(array('post_title' => 'parent-two', 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:02')); $this->parent_three = $this->factory->post->create(array('post_title' => 'parent-three', 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:03')); $this->factory->post->create(array('post_title' => 'child-one', 'post_parent' => $this->parent_one, 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:04')); $this->factory->post->create(array('post_title' => 'child-two', 'post_parent' => $this->parent_one, 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:05')); $this->factory->post->create(array('post_title' => 'child-three', 'post_parent' => $this->parent_two, 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:06')); $this->factory->post->create(array('post_title' => 'child-four', 'post_parent' => $this->parent_two, 'post_type' => 'page', 'post_date' => '2007-01-01 00:00:07')); // Force refresh the index so the data is available immediately SP_API()->post('_refresh'); }
/** * @dataProvider meta_sample_data */ function test_mapping_post_meta($value, $boolean, $long, $double, $datetime) { $demo_post_id = $this->factory->post->create(array('post_title' => rand_str(), 'post_date' => '2015-01-02 03:04:05')); // Test the various meta mappings. Ideally, these each would be their // own test, but add_post_meta($demo_post_id, 'mapping_postmeta_test', $value); SP_Sync_Manager()->sync_post($demo_post_id); SP_API()->post('_refresh'); if (!isset($value)) { if (sp_phpunit_is_wp_at_least(4.4)) { $string = array(); } else { $string = array(''); } } elseif (is_array($value)) { $string = array(serialize($value)); } else { $string = array(strval($value)); } $this->assertSame($string, $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.value'), 'Checking meta.value'); $this->assertSame($string, $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.raw'), 'Checking meta.raw'); $this->assertSame(array($boolean), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.boolean'), 'Checking meta.boolean'); if (isset($long)) { $this->assertSame(array($long), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.long'), 'Checking meta.long'); } else { $this->assertSame(array(), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.long'), 'Checking that meta.long is missing'); } if (isset($double)) { $this->assertSame(array($double), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.double'), 'Checking meta.double'); } else { $this->assertSame(array(), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.double'), 'Checking that meta.double is missing'); } if (isset($datetime)) { list($date, $time) = explode(' ', $datetime); $this->assertSame(array($datetime), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.datetime'), 'Checking meta.datetime'); $this->assertSame(array($date), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.date'), 'Checking meta.date'); $this->assertSame(array($time), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.time'), 'Checking meta.time'); } else { $this->assertSame(array(), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.datetime'), 'Checking that meta.datetime is missing'); $this->assertSame(array(), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.date'), 'Checking that meta.date is missing'); $this->assertSame(array(), $this->_search_and_get_field(array(), 'post_meta.mapping_postmeta_test.time'), 'Checking that meta.time is missing'); } }
function setUp() { parent::setUp(); sp_index_flush_data(); $cat = $this->factory->term->create(array('taxonomy' => 'category', 'name' => 'cat-demo')); $tag = $this->factory->term->create(array('taxonomy' => 'post_tag', 'name' => 'tag-demo')); $this->factory->post->create(array('post_title' => 'lorem-ipsum', 'post_date' => '2009-07-01 00:00:00', 'post_category' => array($cat))); $this->factory->post->create(array('post_title' => 'comment-test', 'post_date' => '2009-08-01 00:00:00', 'post_category' => array($cat))); $this->factory->post->create(array('post_title' => 'one-trackback', 'post_date' => '2009-09-01 00:00:00', 'post_category' => array($cat))); $this->factory->post->create(array('post_title' => 'many-trackbacks', 'post_date' => '2009-10-01 00:00:00', 'post_category' => array($cat))); $this->factory->post->create(array('post_title' => 'no-comments', 'post_date' => '2009-10-02 00:00:00')); $this->factory->post->create(array('post_title' => 'one-comment', 'post_date' => '2009-11-01 00:00:00', 'tags_input' => array($tag))); $this->factory->post->create(array('post_title' => 'contributor-post-approved', 'post_date' => '2009-12-01 00:00:00')); $this->factory->post->create(array('post_title' => 'many-comments', 'post_date' => '2010-01-01 00:00:00')); $this->factory->post->create(array('post_title' => 'simple-markup-test', 'post_date' => '2010-02-01 00:00:00', 'tags_input' => array($tag))); $this->factory->post->create(array('post_title' => 'raw-html-code', 'post_date' => '2010-03-01 00:00:00', 'tags_input' => array($tag))); register_post_type('cpt', array('public' => true)); $this->factory->post->create(array('post_title' => 'cpt', 'post_date' => '2010-01-01 00:00:00', 'post_type' => 'cpt')); $this->factory->post->create(array('post_title' => 'lorem-cpt', 'post_date' => '2010-01-01 00:00:00', 'post_type' => 'cpt')); // Force refresh the index so the data is available immediately SP_API()->post('_refresh'); }
/** * Perform an Elasticsearch query. * * @param array $es_args Elsticsearch query DSL as a PHP array. * @return array Raw response from the ES server, parsed by json_Decode. */ public function search($es_args) { $this->es_args = apply_filters('sp_search_query_args', $es_args); $this->search_results = SP_API()->search(json_encode($this->es_args), array('output' => ARRAY_A)); return $this->search_results; }
public function flush() { return SP_API()->delete(); }
function test_query_author_vars() { $author_1 = $this->factory->user->create(array('user_login' => 'author1', 'user_pass' => rand_str(), 'role' => 'author')); $post_1 = $this->factory->post->create(array('post_title' => rand_str(), 'post_author' => $author_1, 'post_date' => '2006-01-04 00:00:00')); $author_2 = $this->factory->user->create(array('user_login' => 'author2', 'user_pass' => rand_str(), 'role' => 'author')); $post_2 = $this->factory->post->create(array('post_title' => rand_str(), 'post_author' => $author_2, 'post_date' => '2006-01-03 00:00:00')); $author_3 = $this->factory->user->create(array('user_login' => 'author3', 'user_pass' => rand_str(), 'role' => 'author')); $post_3 = $this->factory->post->create(array('post_title' => rand_str(), 'post_author' => $author_3, 'post_date' => '2006-01-02 00:00:00')); $author_4 = $this->factory->user->create(array('user_login' => 'author4', 'user_pass' => rand_str(), 'role' => 'author')); $post_4 = $this->factory->post->create(array('post_title' => rand_str(), 'post_author' => $author_4, 'post_date' => '2006-01-01 00:00:00')); // Force refresh the index so the data is available immediately SP_API()->post('_refresh'); $this->assertEquals(array($author_2), $this->search_and_get_field(array('author' => $author_2), 'post_author.user_id')); $this->assertEquals(array($author_1, $author_2, $author_3, $author_4), $this->search_and_get_field(array('author' => array($author_1, $author_2, $author_3, $author_4)), 'post_author.user_id')); $this->assertEquals(array($author_3), $this->search_and_get_field(array('author_name' => 'author3'), 'post_author.user_id')); $this->assertEquals(array($author_1, $author_2, $author_3, $author_4), $this->search_and_get_field(array('author_name' => array('author1', 'author2', 'author3', 'author4')), 'post_author.user_id')); $this->assertEquals(array($author_4, $author_3, $author_2, $author_1), $this->search_and_get_field(array('author_name' => array('author1', 'author2', 'author3', 'author4'), 'orderby' => 'author'), 'post_author.user_id')); }
function test_api_error() { $response = json_decode(SP_API()->request('http://asdf.jkl;/some/bad/url')); $this->assertNotEmpty($response->error); }
function test_singular_index_non_200() { // This domain is used in unit tests, and we'll get a 404 from trying to use it with ES SP_Config()->update_settings(array('host' => 'http://asdftestblog1.files.wordpress.com', 'active' => true)); // Because we changed the host, we have to re-init SP_API SP_API()->setup(); $posts = array($this->factory->post->create(array('post_title' => 'searchpress'))); $this->assertNotEmpty(SP_Sync_Meta()->messages['error']); $this->assertTrue(SP_Sync_Meta()->has_errors()); }
public function full_sync() { if (!isset($_POST['sp_sync_nonce']) || !wp_verify_nonce($_POST['sp_sync_nonce'], 'sp_sync')) { wp_die('You are not authorized to perform that action'); } else { SP_Config()->update_settings(array('must_init' => false, 'active' => false, 'last_beat' => false)); $result = SP_Config()->flush(); if (!isset(SP_API()->last_request['response_code']) || !in_array(SP_API()->last_request['response_code'], array(200, 404))) { wp_redirect(admin_url('tools.php?page=searchpress&error=100')); } else { SP_Config()->create_mapping(); SP_Sync_Manager()->do_cron_reindex(); wp_redirect(admin_url('tools.php?page=searchpress')); } exit; } }
protected function query_es($es_args) { return SP_API()->search(json_encode($es_args), array('output' => ARRAY_A)); }
/** * Do an indexing loop. This is the meat of the process. * * @return bool */ public function do_index_loop() { $sync_meta = SP_Sync_Meta(); $start = $sync_meta->page * $sync_meta->bulk; do_action('sp_debug', '[SP_Sync_Manager] Getting Range'); $posts = $this->get_range($start, $sync_meta->bulk); // Reload the sync meta to ensure it hasn't been canceled while we were getting those posts $sync_meta->reload(); if (!$posts || is_wp_error($posts) || !$sync_meta->running) { return false; } $response = SP_API()->index_posts($posts); do_action('sp_debug', sprintf('[SP_Sync_Manager] Indexed %d Posts', count($posts)), $response); $sync_meta->reload(); if (!$sync_meta->running) { return false; } $sync_meta->processed += count($posts); if ('200' != SP_API()->last_request['response_code']) { # Should probably throw an error here or something $sync_meta->log(new WP_Error('error', __('ES response failed', 'searchpress'), SP_API()->last_request)); $sync_meta->save(); $this->cancel_reindex(); return false; } elseif (!is_object($response) || !isset($response->items) || !is_array($response->items)) { $sync_meta->log(new WP_Error('error', __('Error indexing data', 'searchpress'), $response)); $sync_meta->save(); $this->cancel_reindex(); return false; } else { foreach ($response->items as $post) { // Status should be 200 or 201, depending on if we're updating or creating respectively if (!isset($post->index->status)) { $sync_meta->log(new WP_Error('warning', __("Error indexing post {$post->index->_id}; Response: " . json_encode($post), 'searchpress'), $post)); } elseif (!in_array($post->index->status, array(200, 201))) { $sync_meta->log(new WP_Error('warning', __("Error indexing post {$post->index->_id}; HTTP response code: {$post->index->status}", 'searchpress'), $post)); } else { $sync_meta->success++; } } } $this->total_pages = ceil($this->published_posts / $sync_meta->bulk); $sync_meta->page++; if (!defined('WP_CLI') || !WP_CLI) { if ($sync_meta->processed >= $sync_meta->total || $sync_meta->page > $this->total_pages) { SP_Config()->update_settings(array('active' => true)); $this->cancel_reindex(); } else { $sync_meta->save(); } } return true; }