You are here

function DrupalSolrNodeTestCase::testApacheSolrNodeAddDelete in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 tests/apachesolr_base.test \DrupalSolrNodeTestCase::testApacheSolrNodeAddDelete()
  2. 6.3 tests/apachesolr_base.test \DrupalSolrNodeTestCase::testApacheSolrNodeAddDelete()

File

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

Class

DrupalSolrNodeTestCase

Code

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');
    $nodes_in_tracking_table = db_select($indexer_table, 'aien')
      ->condition('entity_id', $node->nid)
      ->countQuery()
      ->execute()
      ->fetchField();
    if ($nodes_in_tracking_table > 0) {
      $db_node = db_select($indexer_table, 'aien')
        ->condition('entity_id', $node->nid)
        ->fields('aien', array(
        'entity_id',
        'status',
      ))
        ->execute()
        ->fetchObject();
      $this
        ->assertEqual($db_node->status, 0, t('Node @entity_id has status 0', array(
        '@entity_id' => $db_node->entity_id,
      )));
    }
    else {

      // 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($nodes_in_tracking_table, 0, t('No more nodes in the tracking table'));
    }

    // 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.'));
  }
}