You are here

class DrupalSolrNodeTestCase in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 tests/apachesolr_base.test \DrupalSolrNodeTestCase
  2. 7 tests/apachesolr_base.test \DrupalSolrNodeTestCase

Hierarchy

Expanded class hierarchy of DrupalSolrNodeTestCase

File

tests/apachesolr_base.test, line 390
Unit test class that provides tests for base functionality of the Apachesolr Module without having the need of a Solr Server

View source
class DrupalSolrNodeTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Solr Node add, deletion, and document building tests',
      'description' => 'Tests if we can succesfully add and delete nodes',
      'group' => 'ApacheSolr',
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('apachesolr', 'apachesolr_search', 'search', 'apachesolr_test');
  }
  function testApacheSolrNodeAddDelete() {

    // Login as basic user to perform initial content creation.
    // Create an admin user that can bypass revision moderation.
    $permissions = array(
      'access content',
      'search content',
      'administer nodes',
      'administer search',
      'access content overview',
      'bypass node access',
    );
    $admin_user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($admin_user);

    // enable our bundles to be indexed, and clear caches
    apachesolr_index_set_bundles('solr', 'node', array(
      'page',
      'article',
    ));
    entity_info_cache_clear();
    apachesolr_environments_clear_cache();

    // Define types of node bundles that we want to index
    $types = array(
      'page',
      'article',
    );
    foreach ($types as $type) {
      $edit = array();

      // Create a node of the type $type.
      $edit['uid'] = $admin_user->uid;
      $edit['type'] = $type;
      $edit['title'] = $this
        ->randomName(16);
      $node = $this
        ->drupalCreateNode($edit);
      $this
        ->assertTrue(is_object($node) && isset($node->nid), t('Article type @type has been created.', array(
        '@type' => $type,
      )));

      // Check that the node has been created.
      $node = $this
        ->drupalGetNodeByTitle($edit['title']);
      $this
        ->assertTrue($node, t('Created article @type found in database.', array(
        '@type' => $type,
      )));

      // Check that the node has status 1
      $indexer_table = apachesolr_get_indexer_table('node');
      $query = db_select($indexer_table, 'aien')
        ->condition('entity_id', $node->nid)
        ->fields('aien', array(
        'entity_id',
        'status',
      ));
      $db_node = $query
        ->execute()
        ->fetchObject();
      $this
        ->assertEqual($db_node->status, 1, t('Node @entity_id has status 1', array(
        '@entity_id' => $db_node->entity_id,
      )));

      // Delete the node
      $this
        ->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));

      // check if the entity delete does its work. It should have set the
      // status to 0 so it will be deleted when solr comes online
      $indexer_table = apachesolr_get_indexer_table('node');
      $query = db_select($indexer_table, 'aien')
        ->condition('entity_id', $node->nid)
        ->fields('aien', array(
        'entity_id',
        'status',
      ));
      $db_node = $query
        ->execute()
        ->fetchObject();

      // Check that all of the nodes (should only have 1) have status 0, it
      // is set as 0 because it is pending to be deleted
      $this
        ->assertEqual($db_node->status, 0, t('Node @entity_id has status 0', array(
        '@entity_id' => $db_node->entity_id,
      )));

      // Check that all the nodes have been deleted.
      $count = db_select('node', 'n')
        ->condition('n.nid', $node->nid)
        ->countQuery()
        ->execute()
        ->fetchField();
      $this
        ->assertEqual($count, 0, t('No more nodes left in the node table.'));
    }
  }
  function testNodeToDocument() {

    // enable our bundles to be indexed, and clear caches
    apachesolr_index_set_bundles('solr', 'node', array(
      'article',
    ));
    entity_info_cache_clear();
    apachesolr_environments_clear_cache();
    $edit = array();

    // Create a node of the type article.
    $type = 'article';
    $edit['uid'] = 1;
    $edit['type'] = $type;
    $edit['title'] = $this
      ->randomName(16);
    $edit['body'][LANGUAGE_NONE][0]['value'] = 'some other ORDINARY_TEXT ';

    // Make sure the format allows all tags.
    $edit['body'][LANGUAGE_NONE][0]['format'] = 'full_html';
    $tags_to_index = _apachesolr_tags_to_index();

    // Tags that are not boosted normally.
    $other_tags = array(
      'div' => 'tags_inline',
      'span' => 'tags_inline',
    );
    $all_tags = $tags_to_index + $other_tags;
    $tag_content = array();
    foreach ($all_tags as $tag_name => $field_name) {
      $tag_content[$tag_name] = strtoupper($tag_name) . '_TAG_CONTENT';
      if ($tag_name == 'a') {
        $edit['body'][LANGUAGE_NONE][0]['value'] .= "<{$tag_name} href=\"http://example.com\">{$tag_content[$tag_name]}</{$tag_name}> other filler ";
      }
      else {
        $edit['body'][LANGUAGE_NONE][0]['value'] .= "<{$tag_name}>{$tag_content[$tag_name]}</{$tag_name}> dummy text ";
      }
    }
    $node = $this
      ->drupalCreateNode($edit);
    $this
      ->assertTrue(is_object($node) && isset($node->nid), t('Article type @type has been created.', array(
      '@type' => $type,
    )));
    $item = new stdClass();
    $item->entity_id = $node->nid;
    $item->entity_type = 'node';
    $item->bundle = $node->type;
    $env_id = apachesolr_default_environment();
    $docs = apachesolr_index_entity_to_documents($item, $env_id);
    $this
      ->assertEqual(count($docs), 1, 'Only one document from one node');
    $document = end($docs);
    $this
      ->assertTrue(strpos($document->content, 'ORDINARY_TEXT') !== FALSE, "Found in content field expected: ORDINARY_TEXT");
    foreach ($tags_to_index as $tag_name => $field_name) {
      $this
        ->assertTrue(strpos($document->content, $tag_content[$tag_name]) !== FALSE, "Found in content field expected: {$tag_content[$tag_name]}");
      $this
        ->assertTrue(!empty($document->{$field_name}) && strpos($document->{$field_name}, $tag_content[$tag_name]) !== FALSE, "Found in {$field_name} field expected: {$tag_content[$tag_name]}");
      $this
        ->assertTrue(empty($document->{$field_name}) || strpos($document->{$field_name}, 'ORDINARY_TEXT') === FALSE, "NOT Found in {$field_name}: ORDINARY_TEXT");
    }
    foreach ($other_tags as $tag_name => $field_name) {
      $this
        ->assertTrue(strpos($document->content, $tag_content[$tag_name]) !== FALSE, "Found in content field expected: {$tag_content[$tag_name]}");
      $this
        ->assertTrue(empty($document->{$field_name}) || strpos($document->{$field_name}, $tag_content[$tag_name]) === FALSE, "NOT found in {$field_name}: {$tag_content[$tag_name]}");
    }
  }

}

Members