/**
  * Test the wl_entity_get_by_title method.
  */
 function testGetByTitle1()
 {
     // We're starting up with no entities, we expect 0 entities.
     $posts_1 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(0, $posts_1);
     // Create an entity and see that it is found.
     $post_id_2 = wl_create_post('Lorem Ipsum', 'test-entity-2', 'Test Entity', 'draft', WL_ENTITY_TYPE_NAME);
     $posts_2 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(1, $posts_2);
     $this->assertEquals($post_id_2, $posts_2[0]->id);
     // Create another entity and see that it is found too.
     $post_id_3 = wl_create_post('Lorem Ipsum', 'test-entity-3', 'Test Entity', 'publish', WL_ENTITY_TYPE_NAME);
     $posts_3 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(2, $posts_3);
     $this->assertEquals($post_id_2, $posts_3[0]->id);
     $this->assertEquals($post_id_3, $posts_3[1]->id);
     // Create another entity and see that it is NOT found.
     $post_id_4 = wl_create_post('Lorem Ipsum', 'test-entity-4', 'Test Entity 4', 'publish', WL_ENTITY_TYPE_NAME);
     $posts_4 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(2, $posts_3);
     $this->assertEquals($post_id_2, $posts_4[0]->id);
     $this->assertEquals($post_id_3, $posts_4[1]->id);
     // Now make a LIKE search and see that it is found.
     $posts_5 = wl_entity_get_by_title('Test Entity%');
     $this->assertCount(3, $posts_5);
     $this->assertEquals($post_id_2, $posts_5[0]->id);
     $this->assertEquals($post_id_3, $posts_5[1]->id);
     $this->assertEquals($post_id_4, $posts_5[2]->id);
 }
 /**
  * Test the {@link get_alternative_labels} function by creating an entity and checking that the number of alternative
  * labels matches the one we set via {@link save_post}.
  *
  * @since 3.2.0
  */
 function test_get_alternative_labels()
 {
     $entity_service = Wordlift_Entity_Service::get_instance();
     // Create a test entity.
     $entity_id = wl_create_post('This is Entity 1', 'entity-1', 'Entity 1', 'publish', 'entity');
     wl_set_entity_main_type($entity_id, 'http://schema.org/Thing');
     // Check that we have no alternative labels.
     $this->assertCount(0, $entity_service->get_alternative_labels($entity_id));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array('ABC 1', 'ABD 2', 'EFG 3');
     $entity_service->save_post($entity_id, null, null);
     // Check that we have 3 alternative labels.
     $this->assertCount(3, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(2, wl_entity_get_by_title('AB', true));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array('ABC 1', 'ABD 2');
     $entity_service->save_post($entity_id, null, null);
     // Check that we have 2 alternative labels.
     $this->assertCount(2, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(2, wl_entity_get_by_title('AB', true));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array();
     $entity_service->save_post($entity_id, null, null);
     // Check that we have no alternative labels.
     $this->assertCount(0, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(0, wl_entity_get_by_title('AB'));
 }
/**
 * Execute the {@link wl_entity_get_by_title} function via AJAX.
 *
 * @since 3.0.0
 */
function wl_entity_ajax_get_by_title()
{
    // Get the parameter.
    if (!isset($_GET['title']) || empty($_GET['title'])) {
        wp_die('The title parameter is required.');
    }
    $title = $_GET['title'];
    // Get the edit link.
    $post_type_object = get_post_type_object(WL_ENTITY_TYPE_NAME);
    $edit_link = $post_type_object->_edit_link . '&action=edit';
    // Prepare the response with the edit link.
    $response = new StdClass();
    $response->edit_link = $edit_link;
    $response->results = wl_entity_get_by_title($title);
    wl_core_send_json($response);
}
/**
 * Execute the {@link wl_entity_get_by_title} function via AJAX.
 *
 * @since 3.0.0
 */
function wl_entity_ajax_get_by_title()
{
    // Get the title to search.
    if (empty($_GET['title'])) {
        wp_die('The title parameter is required.');
    }
    $title = $_GET['title'];
    // Are we searching for a specific title or for a containing title?
    $autocomplete = isset($_GET['autocomplete']);
    // Are we searching also for the aliases?
    $include_alias = isset($_GET['alias']);
    // Get the edit link.
    $post_type_object = get_post_type_object(Wordlift_Entity_Service::TYPE_NAME);
    $edit_link = $post_type_object->_edit_link . '&action=edit';
    // Prepare the response with the edit link.
    $response = array('edit_link' => $edit_link, 'results' => wl_entity_get_by_title($title, $autocomplete, $include_alias));
    wl_core_send_json($response);
}
 /**
  * Test the wl_entity_get_by_title method.
  */
 function testGetByTitle1()
 {
     // We're starting up with no entities, we expect 0 entities.
     $posts_1 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(0, $posts_1);
     // Create an entity and see that it is found.
     $post_id_2 = wl_create_post('Lorem Ipsum', 'test-entity-2', 'Test Entity', 'draft', Wordlift_Entity_Service::TYPE_NAME);
     $posts_2 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(1, $posts_2);
     $this->assertEquals($post_id_2, $posts_2[0]->id);
     // Create another entity and see that it is found too.
     $post_id_3 = wl_create_post('Lorem Ipsum', 'test-entity-3', 'Test Entity', 'publish', Wordlift_Entity_Service::TYPE_NAME);
     $posts_3 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(2, $posts_3);
     $this->assertEquals($post_id_2, $posts_3[0]->id);
     $this->assertEquals($post_id_3, $posts_3[1]->id);
     // Create another entity and see that it is NOT found.
     $post_id_4 = wl_create_post('Lorem Ipsum', 'test-entity-4', 'Test Entity 4', 'publish', Wordlift_Entity_Service::TYPE_NAME);
     $posts_4 = wl_entity_get_by_title('Test Entity');
     $this->assertCount(2, $posts_3);
     $this->assertEquals($post_id_2, $posts_4[0]->id);
     $this->assertEquals($post_id_3, $posts_4[1]->id);
     // Now make a LIKE search by hacking on the search param and see that it is found.
     $posts_5 = wl_entity_get_by_title('Test Entity%');
     $this->assertCount(3, $posts_5);
     $this->assertEquals($post_id_2, $posts_5[0]->id);
     $this->assertEquals($post_id_3, $posts_5[1]->id);
     $this->assertEquals($post_id_4, $posts_5[2]->id);
     // Now make a LIKE search using the $autocomplete param
     $posts_5 = wl_entity_get_by_title('Test Entity', true);
     $this->assertCount(3, $posts_5);
     $this->assertEquals($post_id_2, $posts_5[0]->id);
     $this->assertEquals($post_id_3, $posts_5[1]->id);
     $this->assertEquals($post_id_4, $posts_5[2]->id);
     // Entity service instance
     $entity_service = Wordlift_Entity_Service::get_instance();
     // Verify non existence of 'an alias'
     $this->assertCount(0, wl_entity_get_by_title('an alias', false, true));
     // Assign alias to entity 2 and verify it gets found
     $entity_service->set_alternative_labels($post_id_2, 'an alias');
     $this->assertCount(1, wl_entity_get_by_title('an alias'));
     // The alias above should not be found if we don't ask for aliases
     $this->assertCount(0, wl_entity_get_by_title('an alias', false, false));
 }