Skip to content
Snippets Groups Projects
AbonneControllerSuggestionAchatTest.php 14.5 KiB
Newer Older
<?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 
 */
require_once 'AbstractControllerTestCase.php';

class AbonneControllerSuggestionAchatAddFormTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		$this->fixture('Class_TypeDoc', ['id' => 6 ,
		'label' => 'Blueray',
		'famille_id' => Class_CodifTypeDoc::VIDEO]);
		$this->fixture('Class_Bib', ['id' => 11,
		'libelle' => 'Bib de la plage']);

		$this->dispatch('/opac/abonne/suggestion-achat-add', true);
	}

	/** @test */
	public function pageTitleShouldBeSuggestionAchat() {
		$this->assertXPathContentContains('//title', 'Suggérer un achat');
	}


	/** @test */
	public function boiteShouldHaveTitleSuggestionAchat() {
		$this->assertXPathContentContains('//div[@class="boiteMilieu"]//h1', 'Suggérer un achat');
	}


	/** @test */
	public function formShouldContainsInputForTitre() {
		$this->assertXPath('//form//input[@name="titre"][@placeholder="ex: Harry Potter à l\'école des sorciers"]');
	/** @test */
	public function formShouldContainsSelectForDocumentType() {
		$this->assertXPath('//form//select[@name="type_doc_id"]');
	}

	
	/** @test */
	public function typeDocBlueRayShouldBePresent() {
		$this->assertXPath('//form//select[@name="type_doc_id"]//option[@value="6"][@label="Blueray"]');
	}

	
	/** @test */
	public function rssShouldNotBePresent() {
		$this->assertNotXpath('//form//select[@name="type_doc_id"]//option[@value="9"][@label="fils rss"]');
	}

	/** @test */
	public function formShouldContainsInputForAuteur() {
		$this->assertXPath('//form//input[@name="auteur"][@placeholder="ex: Joanne Kathleen Rowling"]');
	}


	/** @test */
	public function formShouldContainsInputForDescriptionUrl() {
		$this->assertXPath('//form//input[@type="url"][@name="description_url"][@placeholder="ex: http://fr.wikipedia.org/wiki/Harry_Potter_à_l\'école_des_sorciers"]');
llaffont's avatar
llaffont committed


	/** @test */
	public function formShouldContainsInputForISBN() {
		$this->assertXPath('//form//input[@name="isbn"][@placeholder="ex: 2-07-054127-4"]');
	}

	/** @test */
	public function LibraryInputShouldBeHiddenAndEleven() {
		$this->assertXPath('//form//input[@type="hidden"][@name="bib_id"][@value="11"]');
llaffont's avatar
llaffont committed
	}
	
	/** @test */
	public function formShouldContainsTextAreaForCommentaire() {
		$this->assertXPath('//form//textarea[@name="commentaire"]');
	}


	/** @test */
	public function formShouldContainsSubmitButtonEnvoyer() {
		$this->assertXPath('//form//input[@type="submit"][@value="Envoyer"]');
	}
class AbonneControllerSuggestionAchatAddFormMultipleBibsTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		$this->fixture('Class_Bib', ['id' => 11,
		'libelle' => 'Bib de la plage']);
		$this->fixture('Class_Bib', ['id' => 12,
		'libelle' => 'Bib de la plage 2']);

		$this->fixture('Class_Users', [
			'id' => 3,
			'login' => 'test',
			'password' => 'test',
			'id_site' => 12,
		]);

		$identity = new StdClass();
		$identity->ID_USER = 3;
		ZendAfi_Auth::getInstance()->getStorage()->write($identity);

		$this->dispatch('/opac/abonne/suggestion-achat-add', true);
	}

	/** @test */
	public function formShouldContainsSelectForLibrary() {
		$this->assertXPath('//form//select[@name="bib_id"]');	
	}

	
	/** @test */
	public function LibrarySelectShouldContainBibDeLaPlage() {
		$this->assertXPath('//form//select[@name="bib_id"]//option[@value="11"][@label="Bib de la plage"]');
		$this->assertXPath('//form//select[@name="bib_id"]//option[@value="12"][@label="Bib de la plage 2"]');
	}

	/** @test */
	public function librarySelectShouldHaveDefaultSelected() {
		$this->assertXPath('//form//select[@name="bib_id"]//option[@value="12"][@selected="selected"]');	
	}

class AbonneControllerSuggestionAchatAddPostTest extends AbstractControllerTestCase {
	protected $_suggestion;
	protected $_mail;

	protected function _loginHook($account) {
		$account->username     = 'Arnaud';
		$account->ID_USER      = 666;
	}

	public function setUp() {
		parent::setUp();
		$codif_type_doc=$this->fixture('Class_CodifTypeDoc', ['id' => 102,
																					'famille_id' => Class_CodifTypeDoc::LIVRE,
																					'bibliotheques' => '2;3',
																					'annexes' => '4;8',
																					'sections' => '9;10']);

		$this->fixture('Class_TypeDoc', ['id'=>Class_TypeDoc::LIVRE, 
		                                  'codif_type_doc' =>  $codif_type_doc,
																		 'label'=> 'Livres Numériques']);

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_SuggestionAchat')
			->whenCalled('save')
			->willDo(
							 function($suggestion){
								 $this->_suggestion = $suggestion->setId(66);
							 });
	}
}


class AbonneControllerSuggestionAchatAddPostValidDataTest extends AbonneControllerSuggestionAchatAddPostTest {

	public function setUp() {
		parent::setUp();

		$this->fixture('Class_Bib', ['id' => 11,
																 'libelle' => 'Bib de la plage',
																 'mail' => 'albert@jean.fr',
																 'mail_suggestions' => '']);
		$mock_transport = new MockMailTransport();
		Zend_Mail::setDefaultTransport($mock_transport);
		$this->postDispatch('/opac/abonne/suggestion-achat-add', 
												['titre' => 'Harry Potter',
												 'auteur' => 'J.K.Rowling',
												 'type_doc_id' => Class_TypeDoc::LIVRE,
												 'description_url' => 'http://harrypotter.fr',
												 'isbn' => '2-07-0541 27_4',
												 'commentaire' => 'Je veux le lire',
												 'submit' => 'Envoyer']);	

		$this->_mail = $mock_transport->sent_mail;
	}


	/** @test */
	public function newSuggestionShouldHaveTitreHarryPotter() {
		$this->assertEquals('Harry Potter', $this->_suggestion->getTitre());
	}


	/** @test */
	public function newSuggestionShouldHaveAuteurJKRowling() {
		$this->assertEquals('J.K.Rowling', $this->_suggestion->getAuteur());
	}


	/** @test */
	public function newSuggetionShouldHaveDescriptionUrlHarryPotterDotFr() {
		$this->assertEquals('http://harrypotter.fr', $this->_suggestion->getDescriptionUrl());
	}


	/** @test */
	public function newSuggestionsShouldHaveIsbn2070541274() {
		$this->assertEquals('2070541274', $this->_suggestion->getIsbn());
	}


	/** @test */
	public function newSuggestionsShouldHaveDocumentType2() {
		$this->assertEquals('Livres Numériques', $this->_suggestion->getTypeDoc()->getLibelle());
	}


	/** @test */
	public function shouldNotHaveSubmitAttribute() {
		$this->assertNotContains('submit', array_keys($this->_suggestion->toArray()));
	}

	
	/** @test */
	public function newSuggestionShouldHaveCommentaireJeVeuxLeLire() {
		$this->assertEquals('Je veux le lire', $this->_suggestion->getCommentaire());
	}


	/** @test */
	public function newSuggestionShouldBelongsToArnaud() {
		$this->assertEquals('Arnaud', $this->_suggestion->getUser()->getLogin());
	}

	
	/** @test */
	public function newSuggetionShouldDateCreationShouldBeToday() {
		$this->assertEquals(date('Y-m-d'), $this->_suggestion->getDateCreation());
	}


	/** @test */
	public function responseShouldRedirectToSuggestionAchatId66() {
		$this->assertRedirectTo('/opac/abonne/suggestion-achat-add/id/66');


	/** @test */
	public function sentMailSubjectShouldBeSuggestionAchatHarryPotter() {
		$this->assertEquals('Suggestion d\'achat: Harry Potter', $this->_mail->getSubject());
	}


	/** @test */
	public function fromShouldBeNoReplyAtLocalhost() {
		$this->assertEquals('noreply@localhost', $this->_mail->getFrom());
	}
	/** @test */
	public function toShouldBeJeanJeanFr() {
		$this->assertEquals('albert@jean.fr', $this->_mail->getRecipients()[0]);
	}


	/** @test */
	public function notificationShouldBeEmpty() {
		$this->assertFalse(isset($_SESSION['FlashMessenger']));
	}

}




class AbonneControllerSuggestionAchatAddPostWithMailSuggestions extends AbonneControllerSuggestionAchatAddPostTest {
	public function setUp() {
		parent::setUp();
		
		$this->fixture('Class_Bib', ['id' => 11,
																 'libelle' => 'Bib de la plage',
																 'mail' => 'jean@jean.fr',
																 'mail_suggestions' => 'jean@jean.fr']);

		$mock_transport = new MockMailTransport();
		Zend_Mail::setDefaultTransport($mock_transport);

		$this->postDispatch('/opac/abonne/suggestion-achat-add', 
												['titre' => 'Harry Potter',
												 'auteur' => 'J.K.Rowling',
												 'type_doc_id' => Class_TypeDoc::LIVRE,
												 'description_url' => 'http://harrypotter.fr',
												 'isbn' => '2-07-0541 27_4',
												 'bib_id' => '11',
												 'commentaire' => 'Je veux le lire',
												 'submit' => 'Envoyer']);	

		$this->_mail = $mock_transport->sent_mail;
	}


	/** @test */
	public function toShouldBeJeanJeanFr() {
		$this->assertEquals('jean@jean.fr', $this->_mail->getRecipients()[0]);
	}
}




class AbonneControllerSuggestionAchatAddPostValidDataButNoMailSentTest extends AbstractControllerTestCase {
	protected $_suggestion;
	protected $_mail;

	protected function _loginHook($account) {
		$account->username     = 'Arnaud';
		$account->ID_USER      = 666;
	}


	public function setUp() {
		parent::setUp();

		Class_Users::getIdentity()->setMail('');
		Class_Profil::getPortail()->setMailSite('');
		Class_Profil::getCurrentProfil()->setMailSite('');

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_SuggestionAchat')
			->whenCalled('save')
			->willDo(
							 function($suggestion){
								 $this->_suggestion = $suggestion->setId(66);
							 });

		$mock_transport = new MockMailTransport();
		Zend_Mail::setDefaultTransport($mock_transport);
		$mock_transport->onSendDo(function() {throw new Zend_Mail_Exception('some error');});
		$this->postDispatch('/opac/abonne/suggestion-achat-add', 
												['titre' => 'Harry Potter',
												 'description_url' => '',
												 'isbn' => '',
												 'commentaire' => '',
												 'submit' => 'Envoyer'],
												true);

		$this->_mail = $mock_transport->sent_mail;
	}


	/** @test */
	public function notificationShouldContainsAucunCourrielEnvoye() {
		$this->assertFlashMessengerContains('Aucun courriel envoyé, erreur: some error');
	}


	/** @test */
	public function newSuggestionShouldHaveTitreHarryPotter() {
		$this->assertEquals('Harry Potter', $this->_suggestion->getTitre());
	}
class AbonneControllerSuggestionAchatAddPostWrongDataTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_SuggestionAchat')
			->whenCalled('save')
			->answers(true);


		$this->postDispatch('/opac/abonne/suggestion-achat-add', 
												['titre' => '',
												 'auteur' => '',
												 'description_url' => 'h p',
												 'isbn' => '207',
												 'commentaire' => '']);	
	}


	/** @test */
	public function errorForTitreAndCommentaireShouldBeTitreOuCommentaireRequis() {
		$this->assertXPathContentContains('//li', 'Titre ou commentaire requis');
	}


	/** @test */
	public function errorForDescriptionUrlShouldBeURLInvalide() {
		$this->assertXPathContentContains('//li', '\'h p\' n\'est pas une URL valide');
	}


	/** @test */
	public function errorForIsbnShouldBeFormatIncorrect() {
		$this->assertXPathContentContains('//li', '\'207\' n\'est pas un ISBN valide');
	}
}




class AbonneControllerSuggestionAchatAddDataWithEmptyFieldsNotRequiredTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_SuggestionAchat')
			->whenCalled('save')
			->answers(true);

		$this->postDispatch('/opac/abonne/suggestion-achat-add', 
												['titre' => 'Millenium',
												 'auteur' => 'Stieg Larsson',
												 'description_url' => '',
												 'isbn' => '',
												 'commentaire' => ''],
												true);	
	}


	/** @test */
	public function responseShouldBeARedirect() {
		$this->assertRedirect();
	}
}




class AbonneControllerSuggestionAchatAddWithIdTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();
    Class_SuggestionAchat::newInstanceWithId(66);
		$this->dispatch('/opac/abonne/suggestion-achat-add/id/66', true);
	}


  /** @test */
	public function pageShouldDisplaySuggestionPriseEnCompte() {
		$this->assertXPathContentContains('//p', 'Votre suggestion d\'achat');
	}

	
	/** @test **/
	public function pageShouldContainsALinkToSuggestionAchat() {
		$this->assertXPath('//a[contains(@href, "/abonne/suggestion-achat")]',$this->_response->getBody());
	}



class AbonneControllerSuggestionAchatWithOutSuggestionTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		$this->dispatch('/opac/abonne/suggestion-achat', true);
	}


  /** @test */
	public function pageShouldDisplayLinkForSuggererSuggestion() {
		$this->assertXPath('//a[contains(@href,"/abonne/suggestion-achat-add")]');
	}

	
	/** @test **/
	public function pageShouldDisplayPasDeSuggestion() {
		$this->assertXpathContentContains('//div/span', 'Vous n\'avez pas encore fait de suggestion.',$this->_response->getBody());
	}

}



class AbonneControllerSuggestionAchatWithOneSuggestionTest extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		$user = $this->fixture('Class_Users',
		['id' => 1,
		'login' => 'Tom',
		'password' => 'pwd']);

		ZendAfi_Auth::getInstance()->logUser($user);

		
		$suggestion = $this->fixture('Class_SuggestionAchat',
		['id' => 1,
		'commentaire' => 'New edition is available.',
		'description_url' => '',
		'isbn' => '',
		'type_doc_id' => Class_CodifTypeDoc::VIDEO]);

		$user->setSuggestionAchat([$suggestion])->save();

		$this->dispatch('/opac/abonne/suggestion-achat', true);
	}

	
	/** @test */
	public function loggedUserShouldHaveOneSuggestion() {
		$this->assertXPathContentContains('//dl/dt', 'Commentaire');
	}


	/** @test */
	public function suggestionShouldHaveTypeDocDVD() {
		$this->assertXPathContentContains('//dl//dd', 'DVD');
	}
}