/** * Set up the test. */ function setUp() { parent::setUp(); // Configure WordPress with the test settings. wl_configure_wordpress_test(); // Empty the blog. wl_empty_blog(); // Creating 2 fake entities $entities = array(wl_create_post('content', 'entity1', 'title1', 'publish', 'entity'), wl_create_post('content', 'entity2', 'title2', 'publish', 'entity')); // Creating a fake post self::$FIRST_POST_ID = wl_create_post('content', 'post1', 'title1', 'publish', 'post'); wl_core_add_relation_instances(self::$FIRST_POST_ID, WL_WHAT_RELATION, $entities); // Creating another fake post and entity (the most connected one) // Creating a fake post $new_post = wl_create_post('content', 'post2', 'title2', 'publish', 'post'); // Create the most connected entity self::$MOST_CONNECTED_ENTITY_ID = wl_create_post('content', 'entity2', 'title2', 'publish', 'entity'); wl_core_add_relation_instance($new_post, WL_WHAT_RELATION, self::$MOST_CONNECTED_ENTITY_ID); wl_core_add_relation_instance(self::$FIRST_POST_ID, WL_WHAT_RELATION, self::$MOST_CONNECTED_ENTITY_ID); }
public function testPostsSelectionStartingFromAnEntity() { // Create 2 posts and 2 entities $entity_1_id = wl_create_post('', 'entity0', 'An Entity', 'draft', 'entity'); $post_1_id = wl_create_post('', 'post1', 'A post', 'publish'); // Insert relations wl_core_add_relation_instance($post_1_id, WL_WHAT_RELATION, $entity_1_id); // Set $_GET variable: this means we will perform data selection for $entity_1_id $_GET['post_id'] = $entity_1_id; // Mock php://input $mock_http_raw_data = json_encode(array()); try { $this->_handleAjax('wordlift_related_posts', $mock_http_raw_data); } catch (WPAjaxDieContinueException $e) { } $response = json_decode($this->_last_response); $this->assertInternalType('array', $response); $this->assertCount(1, $response); $this->assertEquals('post', $response[0]->post_type); $this->assertEquals($post_1_id, $response[0]->ID); }
public function testFacetsSelection() { // Create 2 posts and 2 entities $entity_1_id = wl_create_post('', 'entity0', 'An Entity', 'draft', 'entity'); $entity_2_id = wl_create_post('', 'entity1', 'Another Entity', 'draft', 'entity'); $post_1_id = wl_create_post('', 'post1', 'A post', 'publish'); $post_2_id = wl_create_post('', 'post2', 'A post', 'publish'); // Insert relations wl_core_add_relation_instance($post_1_id, WL_WHAT_RELATION, $entity_1_id); wl_core_add_relation_instance($post_2_id, WL_WHAT_RELATION, $entity_1_id); wl_core_add_relation_instance($post_2_id, WL_WHAT_RELATION, $entity_2_id); // Set $_GET variable: this means we will perform data selection for $entity_1_id $_GET['entity_id'] = $entity_1_id; $_GET['type'] = 'facets'; try { $this->_handleAjax('wl_faceted_search'); } catch (WPAjaxDieContinueException $e) { } $response = json_decode($this->_last_response); $this->assertInternalType('array', $response); $this->assertCount(1, $response); $entity_uris = array($response[0]->id); $this->assertNotContains(wl_get_entity_uri($entity_1_id), $entity_uris); $this->assertContains(wl_get_entity_uri($entity_2_id), $entity_uris); }
/** * Create multiple relation instances * @uses wl_add_relation_instance() to create each single instance * * @param int $subject_id The post ID | The entity post ID. * @param string $predicate Name of the relation: 'what' | 'where' | 'when' | 'who' * @param array $object_ids The entity post IDs collection. * * @return (integer|boolean) Return the relation instances IDs or false */ function wl_core_add_relation_instances($subject_id, $predicate, $object_ids) { // Checks on subject and object if (!is_numeric($subject_id)) { return false; } // Checks on the given relation if (!wl_core_check_relation_predicate_is_supported($predicate)) { return false; } // Check $object_ids is an array if (!is_array($object_ids) || empty($object_ids)) { return false; } // Call method to check and add each single relation $inserted_records_ids = array(); foreach ($object_ids as $object_id) { $new_record_id = wl_core_add_relation_instance($subject_id, $predicate, $object_id); $inserted_records_ids[] = $new_record_id; } return $inserted_records_ids; }
/** * Create: * * 2 Post * * 2 Event entities referenced, one per Post * * 1 Place entity as a distractor * Check that the 2 events are retrieved from the global timeline (no post specified). */ function testGlobalTimeline() { // Create posts $post_1_id = wl_create_post('', 'post-1', 'Post 1', 'publish', 'post'); $post_2_id = wl_create_post('', 'post-2', 'Post 2', 'publish', 'post'); // Create entities (2 events and one place) $entity_1_id = wl_create_post("Entity 1's Text", 'entity-1', "Entity 1's Title", 'publish', 'entity'); wl_set_entity_main_type($entity_1_id, 'http://schema.org/Event'); add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2014-01-01', true); add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2014-01-07', true); $entity_2_id = wl_create_post("Entity 2's Text", 'entity-2', "Entity 2's Title", 'publish', 'entity'); wl_set_entity_main_type($entity_2_id, 'http://schema.org/Event'); add_post_meta($entity_2_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2014-01-02', true); add_post_meta($entity_2_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2014-01-08', true); $entity_3_id = wl_create_post('Entity 3 Text', 'entity-3', 'Entity 3 Title', 'publish', 'entity'); wl_set_entity_main_type($entity_2_id, 'http://schema.org/Place'); add_post_meta($entity_3_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 45.12, true); add_post_meta($entity_3_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 90.3, true); wl_core_add_relation_instances($post_1_id, WL_WHAT_RELATION, array($entity_1_id, $entity_3_id)); wl_core_add_relation_instance($post_2_id, WL_WHAT_RELATION, $entity_2_id); // Call retrieving function with null argument (i.e. global timeline) $events = wl_shortcode_timeline_get_events(null); $this->assertCount(2, $events); $event_ids = array_map(function ($item) { return $item->ID; }, $events); $this->assertContains($entity_1_id, $event_ids); $this->assertContains($entity_2_id, $event_ids); }
/** * Save the post to the triple store. Also saves the entities locally and on the triple store. * * @since 3.0.0 * * @param int $post_id The post id being saved. */ function wl_linked_data_save_post_and_related_entities($post_id) { // Ignore auto-saves if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // get the current post. $post = get_post($post_id); remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); wl_write_log("[ post id :: {$post_id} ][ autosave :: false ][ post type :: {$post->post_type} ]"); // Store mapping between tmp new entities uris and real new entities uri $entities_uri_mapping = array(); // Store classification box mapping $entities_predicates_mapping = null; // Save the entities coming with POST data. if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) { wl_write_log("[ post id :: {$post_id} ][ POST(wl_entities) :: "); wl_write_log(json_encode($_POST['wl_entities'])); wl_write_log("]"); wl_write_log("[ post id :: {$post_id} ][ POST(wl_boxes) :: "); wl_write_log(json_encode($_POST['wl_boxes'], true)); wl_write_log("]"); $entities_via_post = $_POST['wl_entities']; $boxes_via_post = $_POST['wl_boxes']; foreach ($entities_via_post as $entity_uri => $entity) { // Local entities have a tmp uri with 'local-entity-' // These uris need to be rewritten here and replaced in the content if (preg_match('/^local-entity-.+/', $entity_uri) > 0) { // Build the proper uri $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($entity['label'])); // Populate the mapping $entities_uri_mapping[$entity_uri] = $uri; // Override the entity obj $entities_via_post[$entity_uri]['uri'] = $uri; } } // Populate the $entities_predicates_mapping // Local Redlink uris need to be used here foreach ($boxes_via_post as $predicate => $entity_uris) { foreach ($entity_uris as $entity_uri) { wl_write_log("Going to map predicates for uri {$entity_uri} "); // Retrieve the entity label needed to build the uri $label = $entities_via_post[stripslashes($entity_uri)]['label']; $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label)); wl_write_log("Going to map predicate {$predicate} to uri {$uri} "); $entities_predicates_mapping[$uri][] = $predicate; } } // Save entities and push them to Redlink // TODO: pass also latitude, longitude, etc. wl_save_entities(array_values($entities_via_post), $post_id); } // Replace tmp uris in content post if needed $updated_post_content = $post->post_content; // Save each entity and store the post id. foreach ($entities_uri_mapping as $tmp_uri => $uri) { $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content); } // Update the post content wp_update_post(array('ID' => $post->ID, 'post_content' => $updated_post_content)); // Extract related/referenced entities from text. $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content); // Reset previously saved instances wl_core_delete_relation_instances($post_id); // Save relation instances foreach (array_unique($disambiguated_entities) as $referenced_entity_id) { wl_write_log(" Going to manage relation between Post {$post_id} and {$referenced_entity_id}"); if ($entities_predicates_mapping) { wl_write_log(" Going to manage relation instances according to the following mapping"); // Retrieve the entity uri $referenced_entity_uri = wl_get_entity_uri($referenced_entity_id); // Retrieve predicates for the current uri if (isset($entities_predicates_mapping[$referenced_entity_uri])) { foreach ($entities_predicates_mapping[$referenced_entity_uri] as $predicate) { wl_write_log(" Going to add relation with predicate {$predicate}"); wl_core_add_relation_instance($post_id, $predicate, $referenced_entity_id); } } else { wl_write_log("Entity uri {$referenced_entity_uri} missing in the mapping"); wl_write_log($entities_predicates_mapping); } } else { // Just for unit tests wl_core_add_relation_instance($post_id, 'what', $referenced_entity_id); } // TODO Check if is needed wl_linked_data_push_to_redlink($referenced_entity_id); } // Push the post to Redlink. wl_linked_data_push_to_redlink($post->ID); add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities'); }
function testWlCoreGetRelatedEntityIdsForAnEntity() { // Create 2 posts and 1 entities $entity_0_id = wl_create_post('', 'entity0', 'An Entity', 'draft', 'entity'); $entity_1_id = wl_create_post('', 'entity1', 'An Entity', 'draft', 'entity'); $entity_2_id = wl_create_post('', 'entity2', 'An Entity', 'draft', 'entity'); // Insert relations wl_core_add_relation_instance($entity_0_id, WL_WHERE_RELATION, $entity_1_id); wl_core_add_relation_instance($entity_0_id, WL_WHO_RELATION, $entity_2_id); // Check relation are retrieved as expected $result = wl_core_get_related_entity_ids($entity_0_id); $this->assertCount(2, $result); $this->assertTrue(in_array($entity_1_id, $result)); $this->assertTrue(in_array($entity_2_id, $result)); $result = wl_core_get_related_entity_ids($entity_0_id, array('predicate' => WL_WHERE_RELATION)); $this->assertCount(1, $result); $this->assertTrue(in_array($entity_1_id, $result)); $result = wl_core_get_related_entity_ids($entity_0_id, array('predicate' => WL_WHO_RELATION)); $this->assertCount(1, $result); $this->assertTrue(in_array($entity_2_id, $result)); }
/** * Create: * * 2 Posts * * 1 Place entity referenced by both posts * * Check that the geomap popup of the place contains a link to the two posts. */ function testPlacePopupRelatedPosts() { // Create two posts. $post_id_1 = wl_create_post('', 'post-1', 'Post 1', 'publish', 'post'); $post_id_2 = wl_create_post('', 'post-2', 'Post 2', 'publish', 'post'); // Create a place- $place_id = wl_create_post("Entity 1 Text", 'entity-1', "Entity 1 Title", 'publish', 'entity'); wl_set_entity_main_type($place_id, 'http://schema.org/Place'); add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 40.12, true); add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 72.3, true); // Reference place from both posts- wl_core_add_relation_instance($post_id_1, WL_WHERE_RELATION, $place_id); wl_core_add_relation_instance($post_id_2, WL_WHERE_RELATION, $place_id); // Check referencing. $places = wl_shortcode_geomap_get_places($post_id_1); $this->assertCount(1, $places); $this->assertEquals($places[0]->ID, $place_id); // Check json formatted data. $response = wl_shortcode_geomap_prepare_map($places); // Check object attributes $poi = $response['features'][0]; $this->assertTrue(isset($poi)); $this->assertTrue(isset($poi['properties'])); $this->assertTrue(isset($poi['properties']['popupContent'])); // Check if popup contains links to the two posts $popup = $poi['properties']['popupContent']; $link1 = esc_attr(get_permalink($post_id_1)); $this->assertContains($link1, $popup); $link2 = esc_attr(get_permalink($post_id_2)); $this->assertContains($link2, $popup); // Check no thumbnail has been echoed $this->assertNotContains('<img', $popup); }