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'); }
/** * Do an indexing iteration. This fires through the cron. */ public function reindex() { do_action('sp_debug', '[SP Cron] Reindexing'); SP_Sync_Manager()->do_index_loop(); if (SP_Sync_Meta()->running) { $this->schedule_reindex(); } }
/** * @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 test_settings_page() { $current_user = get_current_user_id(); // Ensure that editors don't have access to the settings wp_set_current_user($this->factory->user->create(array('role' => 'editor'))); try { ob_start(); SP_Admin()->sync(); ob_end_clean(); } catch (WPDieException $e) { ob_end_clean(); $this->assertEquals('You do not have sufficient permissions to access this page.', $e->getMessage()); } // ... and that admins do wp_set_current_user($this->factory->user->create(array('role' => 'administrator'))); ob_start(); SP_Admin()->sync(); $contents = ob_get_clean(); // Keep it simple, just make sure there's HTML $this->assertContains('<h2>SearchPress</h2>', $contents); wp_set_current_user($current_user); SP_Sync_Manager()->published_posts = false; }
function test_cron_index_non_200() { $posts = array($this->factory->post->create(array('post_title' => 'test one')), $this->factory->post->create(array('post_title' => 'test two')), $this->factory->post->create(array('post_title' => 'test three')), $this->factory->post->create(array('post_title' => 'test four')), $this->factory->post->create(array('post_title' => 'test five')), $this->factory->post->create(array('post_title' => 'test six')), $this->factory->post->create(array('post_title' => 'test seven')), $this->factory->post->create(array('post_title' => 'test eight')), $this->factory->post->create(array('post_title' => 'searchpress'))); sp_index_flush_data(); // 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' => false)); // Because we changed the host, we have to re-init SP_API SP_API()->setup(); SP_Sync_Manager()->do_cron_reindex(); SP_Sync_Meta()->bulk = 3; SP_Sync_Meta()->save(); $this->assertNotEmpty(wp_next_scheduled('sp_reindex')); $this->_fake_cron(); $this->assertNotEmpty(SP_Sync_Meta()->messages['error']); }
public function cancel_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', 'searchpress')); } else { SP_Sync_Manager()->cancel_reindex(); wp_redirect(admin_url('tools.php?page=searchpress&cancel=1')); exit; } }
/** * Index a single post in elasticsearch and output debugging information. You should enable SP_DEBUG and SAVEQUERIES before running this. * * @synopsis <post_id> */ public function debug($args) { if (empty($args[0])) { WP_CLI::error("Invalid post ID"); } $post_id = intval($args[0]); if (!$post_id) { WP_CLI::error("Invalid post ID"); } global $wpdb; $timestamp_start = microtime(true); WP_CLI::line("Indexing post {$post_id}"); SP_Sync_Manager()->sync_post($post_id); WP_CLI::success("Index complete!"); print_r($wpdb->queries); $this->finish($timestamp_start); }
class ES_WP_Query extends ES_WP_Query_Wrapper { protected function query_es($es_args) { return SP_API()->search(json_encode($es_args), array('output' => ARRAY_A)); } } function sp_es_field_map($es_map) { return wp_parse_args(array('post_name' => 'post_name.raw', 'post_title' => 'post_title.raw', 'post_title.analyzed' => 'post_title', 'post_content.analyzed' => 'post_content', 'post_author' => 'post_author.user_id', 'post_date' => 'post_date.date', 'post_date_gmt' => 'post_date_gmt.date', 'post_modified' => 'post_modified.date', 'post_modified_gmt' => 'post_modified_gmt.date', 'post_type' => 'post_type.raw', 'post_meta' => 'post_meta.%s.raw', 'post_meta.analyzed' => 'post_meta.%s.value', 'post_meta.signed' => 'post_meta.%s.long', 'post_meta.unsigned' => 'post_meta.%s.long', 'term_name' => 'terms.%s.name.raw', 'category_name' => 'terms.%s.name.raw', 'tag_name' => 'terms.%s.name.raw'), $es_map); } add_filter('es_field_map', 'sp_es_field_map'); if (defined('ES_WP_QUERY_TEST_ENV') && ES_WP_QUERY_TEST_ENV) { remove_action('save_post', array(SP_Sync_Manager(), 'sync_post')); remove_action('delete_post', array(SP_Sync_Manager(), 'delete_post')); remove_action('trashed_post', array(SP_Sync_Manager(), 'delete_post')); function es_wp_query_index_test_data() { // If your ES server is not at localhost:9200, you need to set $_ENV['searchpress_host']. $host = !empty($_ENV['searchpress_host']) ? $_ENV['searchpress_host'] : 'http://localhost:9200'; SP_Config()->update_settings(array('active' => false, 'host' => $host)); SP_API()->index = 'es-wp-query-tests'; SP_Config()->flush(); SP_Config()->create_mapping(); $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'any', 'post_status' => array_values(get_post_stati()), 'orderby' => 'ID', 'order' => 'ASC')); $sp_posts = array(); foreach ($posts as $post) { $sp_posts[] = new SP_Post($post); } $response = SP_API()->index_posts($sp_posts); if ('200' != SP_API()->last_request['response_code']) {
/** * Get information about a post author * * @param int $user_id * @return array */ public function get_user($user_id) { if (!empty(SP_Sync_Manager()->users[$user_id])) { return SP_Sync_Manager()->users[$user_id]; } $user = get_userdata($user_id); if ($user instanceof WP_User) { $data = array('user_id' => intval($user_id), 'login' => strval($user->user_login), 'display_name' => strval($user->display_name), 'user_nicename' => strval($user->user_nicename)); } else { $data = array('user_id' => intval($user_id), 'login' => '', 'display_name' => '', 'user_nicename' => ''); } SP_Sync_Manager()->users[$user_id] = $data; do_action('sp_debug', '[SP_Post] Compiled User', $data); return $data; }