<?php
/*
 * @file
 * Tests for contributor handling in the Biblio module
 *
 */

class BiblioContributorWebTestCase extends BiblioWebTestCase {
  function setUp() {
    parent::setUp('biblio');
    require_once(drupal_get_path('module', 'biblio') . '/includes/biblio.contributors.inc');
  }


}

class BiblioContributorUnitTest extends BiblioContributorWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Biblio contributor unit tests',
      'description' => 'Unit tests for contributor functions.',
      'group' => 'Biblio',
    );
  }

  function testGrabSurname() {

    $surname = 'van der Plus';
    list ($surname, $prefix) = _grabSurname($surname);
    $this->assertIdentical($surname, 'Plus' );
    $this->assertIdentical($prefix, 'van der' );
    $surname = 'Van den Bussche';
    list ($surname, $prefix) = _grabSurname($surname);
    $this->assertIdentical($surname, 'Van den Bussche' );
    $this->assertIdentical($prefix, FALSE );
  }
  function testGrabFirstnameInitials() {

    $string = "Ron";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, 'Ron' );
    $this->assertIdentical($initials, '' );
    $string = "Ron J.";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, 'Ron' );
    $this->assertIdentical($initials, 'J' );
    $string = "sir Ron J.";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, 'Ron' );
    $this->assertIdentical($initials, 'J' );
    $this->assertIdentical($prefix, 'sir' );
    $string = "R J";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, '' );
    $this->assertIdentical($initials, 'R J' );
    $string = "R. J.";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, '' );
    $this->assertIdentical($initials, 'R J' );
    $string = "R.J.";
    list($firstname, $initials, $prefix) = _grabFirstnameInitials($string);
    $this->assertIdentical($firstname, '' );
    $this->assertIdentical($initials, 'R J' );

  }

  function testBiblioParseAuthor() {

    $author['name'] = 'Bush, Jr. III, George W';
    $author['auth_category'] = 1;
    $author = biblio_parse_author($author);
    $this->assertIdentical($author['firstname'], 'George', 'Test biblio_parse_author($author), firstname' );
    $this->assertIdentical($author['lastname'], 'Bush', 'Test biblio_parse_author($author), lastname');
    $this->assertIdentical($author['initials'], 'W', 'Test biblio_parse_author($author), initials' );
    $this->assertIdentical($author['suffix'], 'Jr. III', 'Test biblio_parse_author($author), suffix' );

  }

  function testBiblioUpdateContributors() {

    $node = $this->createNode();
    $nid = $node->nid;
    $vid1 = $node->vid;
    $this->assertIdentical($node->biblio_contributors[2]['firstname'], 'George', 'Test biblio_insert_contributors($node), firstname');
    $this->assertIdentical($node->biblio_contributors[2]['lastname'], 'Bushzzzzzz', 'Test biblio_insert_contributors($node), lastname' );
    unset($node->biblio_contributors[2]);
    $node->revision = TRUE;
    node_save($node);

    $node = node_load($nid, NULL, TRUE);
    $this->assertFalse(isset($node->biblio_contributors[2]), 'Test removing an author and updating the node');

    biblio_delete_contributor_revision($node->biblio_contributors[1]['cid'], $node->vid);
    $node = node_load($nid, NULL, TRUE);
    $this->assertEqual(count($node->biblio_contributors), 1, 'Test biblio_delete_contributor_revision($cid, $vid)');

    $node = node_load($nid, $vid1, TRUE);

    $this->assertEqual(count($node->biblio_contributors), 3, 'Test load original vid, still three authors');

    biblio_delete_contributors($node);
    $node = node_load($nid, NULL, TRUE);
    $this->assertFalse(count($node->biblio_contributors), 'Test biblio_delete_contributors($node), should be zero authors on reload');

  }

  function testBiblioDeleteOrphanAuthors() {
    $orphan_authors = array();
    $orphan_authors = biblio_get_orphan_authors(); // first save any existing orphans so we can put them back
    $orphan_count = biblio_count_orphan_authors();
    $author = array('name' => 'Ron J. Jeromezzzzzz',  'auth_type' => 1);
    biblio_save_contributor($author);  // create a new orphan so we will have at least one
    $before_count = biblio_count_orphan_authors();
    $this->assertTrue($before_count != 0, "There are $before_count orphans to delete");
    biblio_delete_orphan_authors(TRUE);
    $after_count = biblio_count_orphan_authors();
    $this->assertEqual($after_count, 0, "There are now $after_count orphans");


    foreach ($orphan_authors as $author) {
      biblio_save_contributor($author);
    }
    $restored_count = biblio_count_orphan_authors();
    $this->assertEqual($orphan_count, $restored_count, "Restored $restored_count of $orphan_count original orphans");

  }

}
