Skip to content
Snippets Groups Projects
BibControllerTest.php 12 KiB
Newer Older
llaffont's avatar
llaffont committed
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
 * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * AFI-OPAC 2.0 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with AFI-OPAC 2.0; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
llaffont's avatar
llaffont committed
 */
require_once 'AbstractControllerTestCase.php';

abstract class BibControllerWithZoneTestCase extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();
		unset($_REQUEST['geo_zone']);
		$_SESSION['selection_bib'] = ['nb_notices' => 12345,
																	'id_bibs' => null];
llaffont's avatar
llaffont committed

		$this->bib_annecy = Class_Bib::getLoader()
			->newInstanceWithId(4)
			->setIdZone(1)
			->setLibelle('Annecy')
			->setAffZone('')
			->setVille('Annecy')
			->setUrlWeb('http://www.annecy.fr')
			->setMail('jp@annecy.com')
			->setTelephone('04 50 51 32 12')
			->setAdresse('1 rue Jean Jaures')
			->setVisibilite(Class_Bib::V_DATA);

		$this->haute_savoie = Class_Zone::getLoader()
			->newInstanceWithId(1)
			->setCouleurTexte('#059')
			->setCouleurOmbre('#234')
			->setTailleFonte('14')
			->setImage('carte_moulins.jpg')
			->setBibs(array($this->bib_annecy));

		$ecrivez_des_tests = Class_Article::getLoader()
			->newInstanceWithId(2)
			->setIdSite(0)
			->setTitre('Ecrivez des tests !')
			->setContenu('Ça fera le plus grand bien')
			->setCategorie(Class_ArticleCategorie::getLoader()
										 ->newInstanceWithId(5)
										 ->setLibelle('Bonnes pratiques'));
llaffont's avatar
llaffont committed
		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Article')
			->whenCalled('getArticlesByPreferences')
			->with(array('id_bib' => '4'))
			->answers(array($ecrivez_des_tests))

			->whenCalled('filterByLocaleAndWorkflow')
			->with(array($ecrivez_des_tests))
			->answers(array($ecrivez_des_tests));


		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Zone')
			->whenCalled('findAll')
			->answers(array($this->haute_savoie));
	}
}



class BibControllerZoneViewOneTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();

		$this->dispatch('bib/zoneview/id/1');
	}

	/** @test */
	public function bibListShouldBeVisible() {
		$this->assertXPathContentContains("//table//td[@class='listeTitre']",
llaffont's avatar
llaffont committed
																			"Bibliothèque");
	}


	/** @test */
	public function actualitesShouldBeVisible() {
		$this->assertXPathContentContains("//h2",	"Actualités :");
	}


	/** @test */
	public function bibAnnecyShouldBeVisible() {
		$this->assertXPathContentContains("//table//td//a",
																			"Annecy");
	}


	/** @test */
	public function bibAnnecyShouldBeVisibleOnMap() {
		$this->assertXPathContentContains("//span[contains(@onclick, '/bib/bibview/id/4')]", "Annecy");
	}


	/** @test */
	public function mapShouldBeVisible() {
		$this->assertXPath("//div[@id='image_container']");
	}


	/** @test */
	public function imageZoneShouldBeVisible() {
		$this->assertXPath("//div[@id='image_container'][contains(@style, 'images/blank.gif')]");
	}


	/** @test */
	public function articleEcrivezDesTestsShouldBeVisible() {
		$this->assertXPathContentContains("//li//a", "Ecrivez des tests !");
	}
}


class BibControllerZoneViewOneWithHideNewsTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();
		Class_Profil::getCurrentProfil()->setCfgModules(array("bib" => array("zoneview" => array("hide_news" => 1))));
		$this->dispatch('bib/zoneview/id/1');
	}


	/** @test */
	public function actualitesShouldNotBeVisible() {
		$this->assertNotXPathContentContains("//h2",	"Actualités :");
	}


	/** @test */
	public function articleEcrivezDesTestsShouldNotBeVisible() {
		$this->assertNotXPathContentContains("//li//a", "Ecrivez des tests !");
	}
}


class BibControllerIndexWithHideNewsTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();
		Class_Profil::getCurrentProfil()->setCfgModules(array("bib" => array("index" => array("hide_news" => 1))));
		$this->dispatch('bib/index');
	}


	/** @test */
	public function actualitesShouldNotBeVisible() {
		$this->assertNotXPathContentContains("//h2",	"Actualités :");
	}


	/** @test */
	public function articleEcrivezDesTestsShouldNotBeVisible() {
		$this->assertNotXPathContentContains("//li//a", "Ecrivez des tests !");
	}
}



class BibControllerIndexWithShowNewsTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();
		Class_Profil::getCurrentProfil()->setCfgModules(array("bib" => array("index" => array("hide_news" => 0))));
		$this->dispatch('bib/index');
	}


	/** @test */
	public function actualitesShouldBeVisible() {
		$this->assertXPathContentContains("//h2",	"Actualités :");
	}


	/** @test */
	public function articleEcrivezDesTestsShouldBeVisible() {
		$this->assertXPathContentContains("//li//a", "Ecrivez des tests !");
	}


	/** @test */
	function hrefForArticleEcrivezDesTestsShouldLinkToArticleView() {
		$this->assertXPath('//li//a[contains(@href, "cms/articleview/id/2")]',
											 $this->_response->getBody());
	}
}


class BibControllerMapViewTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();

		$aff_zone = array('profilID' => '3',
											'libelle' => 'Annecy',
											'posX' => 2,
											'posY' => 5,
											'posPoint' => 'droite');
		$this->bib_annecy->setAffZone(ZendAfi_Filters_Serialize::serialize($aff_zone));

		$this->dispatch('bib/mapzoneview/id/1');
	}


	/** @test */
	public function actionShouldBeMapZoneView() {
		$this->assertAction('mapzoneview');
	}


	/** @test */
	public function mapShouldBeVisible() {
		$this->assertXPath("//div[@id='image_container']");
	}


	/** @test */
	public function imageZoneShouldBeVisible() {
		$this->assertXPath("//div[@id='image_container'][contains(@style, 'images/blank.gif')]");
	}


	/** @test */
	public function bibAnnecyShouldBeVisibleOnMap() {
		$this->assertXPathContentContains("//span[contains(@onclick, '?id_profil')]", "Annecy");
	}
}



class BibControllerBibViewAnnecyTest extends BibControllerWithZoneTestCase {
llaffont's avatar
llaffont committed
	public function setUp() {
		parent::setUp();

		$this->_mock_sql = Storm_Test_ObjectWrapper::mock();
		Zend_Registry::set('sql', $this->_mock_sql);

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Exemplaire')
			->whenCalled('countBy')
			->with(['model' => $this->bib_annecy, 'role' => 'bib'])
			->answers(20)
			->beStrict();
		$this->_mock_sql
			->answers('');

		$this->dispatch('bib/bibview/id/4', true);
	}


	/** @test */
	public function formRechercheShouldLinkToCurrentBib() {
		$this->assertXPath('//form[contains(@action, "/recherche/simple/id/4/geo_bib/4")]', $this->_response->getBody());
llaffont's avatar
llaffont committed
	}


	/** @test */
	function AdresseShouldBe1RueJeanJaures() {
		$this->assertXPathContentContains('//tr', '1 rue Jean Jaures');
	}


	/** @test */
	function articleEcrivezDesTestsShouldBeVisible() {
		$this->assertXPathContentContains('//h2', 'Ecrivez des tests !');
	}

	/** @test */
	function libelleCatBonnesPratiquesShouldBeVisible() {
		$this->assertXPathContentContains('//h1', 'Bonnes pratiques');
	}

	/** @test */
	public function urlRetourShouldBeZoneViewIdOne() {
		$this->assertXPathContentContains('//a[contains(@href, "/bib/zoneview/id/1")]',
																			'Retour');
llaffont's avatar
llaffont committed
	}
}


class BibControllerBibViewAnnecyWithParamRetourHistoriqueTest extends BibControllerWithZoneTestCase {
	/** @test */
	public function withUrlRetourCmsArticleViewFiveShouldBeAccepted() {
		$this->dispatch('bib/bibview/id/4?retour=cms+articleview+5');
		$this->assertXPathContentContains('//a[contains(@href, "/cms/articleview/id/5")]',
																			'Retour');
llaffont's avatar
llaffont committed
	}

	/** @test */
	public function withUrlRetourCmsOnlyShouldNotBeAccepted() {
		$this->dispatch('bib/bibview/id/4?retour=cms');
		$this->assertXPathContentContains('//a[contains(@href, "/bib/zoneview/id/1")]',
																			'Retour');
llaffont's avatar
llaffont committed
	}
}


class BibControllerBibViewInexistantTest extends BibControllerWithZoneTestCase {
	public function setUp() {
		parent::setUp();

		$this->dispatch('bib/bibview/');
	}

	/** @test */
	public function responseShouldRedirectToIndex() {
		$this->assertRedirectTo('/opac/bib/index');
	}
}

abstract class BibControllerSelectionTestCase extends AbstractControllerTestCase {
		public function setUp() {
		unset($_REQUEST['geo_zone']);
		unset($_REQUEST['geo_bib']);
		unset($_SESSION['selection_bib']);
		$_SESSION['selection_bib'] = ['id_bibs' => []];

		$this->fixture('Class_Zone',
									 ['id' => 1,
									  'libelle' => 'Africa']);

		$this->fixture('Class_Zone',
									 ['id' => 2,
									  'libelle' => 'Europe']);


		$cairo = $this->fixture('Class_Bib',
														['id' => 1,
														 'id_site' => 1,
														 'libelle' => 'Cairo',
														 'id_zone' => 1,
														 'visibilite' => 2]);

		$casablanca = $this->fixture('Class_Bib',
																 ['id' => 2,
																	'id_site' => '2',
																	'libelle' => 'Casablanca',
																	'visibilite' => 2,
																	'id_zone' => 1]);

		$paris = $this->fixture('Class_Bib',
																 ['id' => 3,
																	'id_site' => '2',
																	'libelle' => 'Paris',
																	'visibilite' => 2,
																	'id_zone' => 2]);
		}
}



class BibControllerBibSelectionWithEmptySessionTest extends BibControllerSelectionTestCase {
	public function setUp() {
		parent::setUp();
		$this->dispatch('bib/selection', true);
	}


	/** @test */
	public function pageShouldBeHtml5Valid() {
		$this->assertHTML5($this->_response->getBody());
	}


	/** @test */
	public function cairoShouldBeInBibList() {
		$this->assertXPathContentContains('//li/span', 'Cairo');
	}


	/** @test */
	public function casablancaShouldBeInBibList() {
		$this->assertXPathContentContains('//li/span', 'Casablanca');
	}


	/** @test */
	public function inputTypeSubmitShouldBePresent() {
		$this->assertXPath('//form//input[@type="submit"]');
	}
class BibControllerBibSelectionWithBibsIdsInSessionTest extends BibControllerSelectionTestCase {
	public function setUp() {
		parent::setUp();
		$_SESSION['selection_bib'] = ['id_bibs' => [1, 2]];
		$this->dispatch('bib/selection', true);
	}


	/** @test */
	public function cairoShouldBeChecked() {
		$this->assertXPath('//li/input[@checked="checked"][@value="1"]');
	}


	/** @test */
	public function casablancaShouldBeChecked() {
		$this->assertXPath('//li/input[@checked="checked"][@value="2"]');
	}


	/** @test */
	public function parisShouldNotBeChecked() {
		$this->assertNotXPath('//li/input[@checked="checked"][@value="3"]');
	}
}




class BibControllerBibSelectionPostDispatchTest extends BibControllerSelectionTestCase {
	public function setUp() {
		parent::setUp();
		$this->postDispatch('bib/selection', ['bib_select' => [1, 2]]);
	}


	/** @test */
	public function seesionShouldContainsCasablancaAndCairo() {
		$this->assertEquals([1, 2] ,$_SESSION['selection_bib']['id_bibs']);
	}

	/** @test */
	public function sessionShouldNotContainsMessage() {
		$this->assertFalse(array_key_exists('message', $_SESSION['selection_bib']));
	}


	/** @test */
	public function sessionShouldNotContainsHtml() {
		$this->assertFalse(array_key_exists('html', $_SESSION['selection_bib']));
	}
}


class BibControllerBibSelectionWithProfilIdParamTest extends BibControllerSelectionTestCase {
	public function setUp() {
		parent::setUp();
		$this->dispatch('opac/index/index/id_profil/1', true);
	}


	/** @test */
	public function sessionSlectionBibShouldNotExist() {
		$this->assertFalse(array_key_exists('id_bibs', $_SESSION['selection_bib']));
	}
}