<?php

include dirname(__FILE__) . '/../../bootstrap/functional.php';
$browser = new DarwinTestFunctional(new sfBrowser());
$browser->loadData($configuration)->login('root', 'evil');
$vernacular_names = new VernacularNames();
$vernacular_names->setCommunity('French');
$vernacular_names->setReferencedRelation('taxonomy');
$vernacular_names->setRecordId(4);
$vernacular_names->name = 'Faux con';
$vernacular_names->save();
$personId = Doctrine::getTable('People')->findOneByFamilyNameAndGivenName('Root', 'Evil')->getId();
$catalogue_people = new CataloguePeople();
$catalogue_people->setReferencedRelation('taxonomy');
$catalogue_people->setRecordId(4);
$catalogue_people->setPeopleType('authors');
$catalogue_people->setPeopleSubType('Main authors');
$catalogue_people->setPeopleRef($personId);
$catalogue_people->save();
$browser->info('2 - Recombined')->get('/widgets/reloadContent?widget=relationRecombination&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table tbody tr', 1)->checkElement('table tbody tr td:first a.link_catalogue', '/recombinus/')->checkElement('img', 3)->end();
$items = Doctrine::getTable('CatalogueRelationships')->getRelationsForTable('taxonomy', 4, 'current_name');
$browser->get('/catalogue/deleteRelated?table=catalogue_relationships&id=' . $items[0]['id'])->with('response')->begin()->isStatusCode(200)->end();
$browser->info('3 - Comment')->get('/widgets/reloadContent?widget=comment&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table tr', 2)->checkElement('table tr:last a.link_catalogue', '/taxon information/')->checkElement('table tr:last td:nth-child(2)', '/This is bullshit/')->end()->info('4 - Properties')->get('/widgets/reloadContent?widget=properties&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table tbody tr', 2)->checkElement('table tbody tr:first td:first', '/physical measurement/')->checkElement('table tbody tr:last td:first', '/protection status/')->end()->info('5 - Vernacular Names')->get('/widgets/reloadContent?widget=vernacularNames&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table tbody tr', 1)->checkElement('table tbody tr:first td:first', '/French/')->checkElement('table tbody tr:first td::nth-child(2)', '/Faux con/')->end()->info('6 - Synonymies')->get('/widgets/reloadContent?widget=synonym&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table.catalogue_table tbody table[alt="homonym"]', 1)->checkElement('table.catalogue_table tbody table[alt="synonym"]', 1)->checkElement('table.catalogue_table tbody table[alt="synonym"] tbody tr', 2)->checkElement('table.catalogue_table tbody table[alt="synonym"] tbody td:first[class="handle"]', 1)->checkElement('table.catalogue_table tbody table[alt="synonym"] tbody td:nth-child(2)', '/Duchesnus Brulus 1912/')->checkElement('table.catalogue_table tbody table[alt="synonym"] tbody tr:first td:nth-child(3)[class="basio_cell"]', 1)->checkElement('table.catalogue_table tbody table[alt="synonym"] tbody tr:first td:nth-child(4)[class="widget_row_delete"]', 1)->end();
$items = Doctrine::getTable('ClassificationSynonymies')->findOneByReferencedRelationAndRecordId('taxonomy', 4);
$browser->info('8 - Synonymies after a delete')->get('/synonym/delete?id=' . $items['id'])->with('response')->begin()->isStatusCode(200)->matches('/ok/')->end()->get('/widgets/reloadContent?widget=synonym&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table.catalogue_table tbody table[alt="homonym"]', 1)->checkElement('table.catalogue_table tbody table[alt="synonym"]', 0)->end()->info('9 - People related')->get('/widgets/reloadContent?widget=cataloguePeople&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table[class="widget_sub_table"] tbody td:first[class="handle"]', 1)->checkElement('table[class="widget_sub_table"] tbody td:nth-child(2)', '/Root Evil/')->checkElement('table[class="widget_sub_table"] tbody td:nth-child(3)[class="catalogue_people_sub_type"]', 1)->checkElement('table[class="widget_sub_table"] tbody td:last[class="widget_row_delete"]', 1)->end()->click('table[class="widget_sub_table"] tbody td:last[class="widget_row_delete"] > a')->with('response')->begin()->isStatusCode(200)->matches('/ok/')->end()->get('/widgets/reloadContent?widget=cataloguePeople&category=catalogue_taxonomy&eid=4')->with('response')->begin()->isStatusCode(200)->checkElement('table[class="widget_sub_table"]', 0)->end();
$t->is(count($catpeostyp), 2, 'Without giving a type, we have "2" different subtypes');
$t->is($catpeostyp['General'], 'General', 'First sub-type has been well modified in "General"');
$t->is($catpeostyp['Main Author'], 'Main Author', 'Second sub-type is still well "Main Author"');
$t->info('Testing the sub-types with a given type');
$catpeostyp = Doctrine::getTable('CataloguePeople')->getDistinctSubType('author');
$t->is(count($catpeostyp), 6, 'With "author" type, we have "6" different subtypes');
$t->is($catpeostyp['Main Author'], 'Main Author', 'Only sub-type fetched is well "Main Author"');
$catpeostyp = Doctrine::getTable('CataloguePeople')->getDistinctSubType('expert');
$t->is(count($catpeostyp), 1, 'With "expert" type, we have "1" different subtypes');
$t->is($catpeostyp['General'], 'General', 'Only sub-type fetched is well "General"');
$t->info('Associating a new Author: "ML Duchesne"');
$author = new CataloguePeople();
$author->setReferencedRelation('taxonomy');
$author->setRecordId($taxon->getId());
$author->setPeopleType('author');
$author->setPeopleSubType('Main Author');
$author->setPeopleRef($person3->getId());
$author->setOrderBy(1);
$author->save();
$t->info('ML Duchesne as Author associated');
$catpeo = Doctrine::getTable('CataloguePeople')->findForTableByType('taxonomy', $taxon->getId());
$t->is(count($catpeo['author']), 2, 'With author key: "2" record.');
$t->is($catpeo['author'][0]->getPeople()->getGivenName(), 'Poilux', 'Type author and first person well associated to "Poilux"');
$t->is($catpeo['author'][1]->getPeople()->getGivenName(), 'ML', 'Type author and second person well associated to "ML"');
$t->info('Interverting order of "Poilux Duchesne" and "ML Duchesne"');
$catpeo = Doctrine_Query::create()->from('CataloguePeople c')->orderBy('referenced_relation ASC, order_by ASC, id ASC')->execute();
Doctrine::getTable('CataloguePeople')->changeOrder($catpeo[1]->getReferencedRelation(), $catpeo[1]->getRecordId(), 'author', array($catpeo[3]->getId(), $catpeo[1]->getId()));
$catpeo = Doctrine::getTable('CataloguePeople')->findForTableByType('taxonomy', $taxon->getId());
$t->is($catpeo['author'][0]->getPeople()->getGivenName(), 'ML', 'Type author and first person well associated to "ML"');
$t->is($catpeo['author'][1]->getPeople()->getGivenName(), 'Poilux', 'Type author and second person well associated to "Poilux"');
$t->info('Testing the getPeopleRelated method');