<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH 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).
 *
 * BOKEH 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 BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */
require_once 'AbstractControllerTestCase.php';

abstract class AbonneFlorenceIsLoggedControllerTestCase extends AbstractControllerTestCase {
  protected $_storm_default_to_volatile = true;

  protected function _initProfilHook($profil) {
    $profil
      ->setBrowser('opac')
      ->setCfgAccueil('a:2:{s:7:"modules";a:0:{}s:7:"options";a:0:{}}');
  }


  public function setUp() {
    parent::setUp();
    $this->fixture('Class_AdminVar',
                   ['id' => 'AVIS_MIN_SAISIE',
                    'valeur' => 0]);
    $this->fixture('Class_AdminVar',
                   ['id' => 'AVIS_MAX_SAISIE',
                    'valeur' => 1000]);

    $this->florence = $this->fixture('Class_Users',
                                     ['id' => 123456,
                                      'pseudo' => 'FloFlo',
                                      'role_level' => 2,
                                      'role' => 'abonne_sigb',
                                      'login' => 'florence',
                                      'password' => 'caramel',
                                      'id_site' => 1,
                                      'idabon' => '00123',
                                      'fiche_sigb' => ['type_com' => 0]]);

    ZendAfi_Auth::getInstance()->logUser($this->florence);

  }
}



abstract class AbonneControllerAvisTestCase extends AbonneFlorenceIsLoggedControllerTestCase {
  public function setUp() {
    parent::setUp();

    $this->potter = Class_Notice::newInstanceWithId(53,
                                                    ['clef_oeuvre' =>'POTTER']);
    $this->potter->save();
  }
}




class AbonneControllerAvisNoticeWithoutAvisTest extends AbonneControllerAvisTestCase {
  public $xpath, $json;
  public function setUp() {
    parent::setUp();
    $this->xpath = new Storm_Test_XPath();
    $this->dispatch('/opac/abonne/avis/id_notice/53');
    $this->json = json_decode($this->_response->getbody());
  }


  /** @test */
  public function actionControllerShouldBeAbonneAvis() {
    $this->assertController('abonne');
    $this->assertAction('avis');
  }


  /** @test */
  public function inputAvisSignatureShouldContainFloFlo() {
    $this->xpath->assertXPath($this->json->content,"//input[@name='avisSignature'][@value='FloFlo']",$this->_response->getBody());
  }


  /** @test */
  public function formActionShouldBeAvisId53() {
    $this->xpath->assertXPath($this->json->content,"//form[@action='/abonne/avis/id_notice/53']");
  }
}




class AbonneControllerAvisInvalidNoticeAvisSaveTest extends  AbonneControllerAvisTestCase {
  public $xpath,$json;
  public function setUp() {
    parent::setUp();

    $this->avis_min_saisie = new Class_AdminVar();
    $this->avis_min_saisie
      ->setId('AVIS_MIN_SAISIE')
      ->setValeur(10);

    $this->avis_max_saisie = new Class_AdminVar();
    $this->avis_max_saisie
      ->setId('AVIS_MAX_SAISIE')
      ->setValeur(1200);

    Class_AdminVar::getLoader()
      ->cacheInstance($this->avis_min_saisie)
      ->cacheInstance($this->avis_max_saisie);
  }


  public function testAvisTooShort() {
    $data = ['avisEntete' => 'Sorcellerie',
             'avisTexte' => 'On adore',
             'avisNote' => 5,
             'avisSignature' => 'FloCouv'];
    $this->xpath = new Storm_Test_XPath();
    $this->getRequest()
         ->setMethod('POST')
         ->setPost($data);
    $this->dispatch('/opac/abonne/avis/id_notice/53');
    $this->json = json_decode($this->_response->getbody());
    $this->assertController('abonne');
    $this->assertAction('avis');
    $this->xpath->assertXPathContentContains($this->json->content,
                                             '//p[@class="error"]','une longueur comprise entre 10 et 1200',
                                             $this->_response->getBody());
  }

  public function testEmptyEntete() {
    $data = array('avisEntete' => '',
                  'avisTexte' => 'On adore la magie',
                  'avisNote' => 5,
                  'avisSignature' => '');
    $this->xpath = new Storm_Test_XPath();
    $this->getRequest()
         ->setMethod('POST')
         ->setPost($data);
    $this->dispatch('/opac/abonne/avis/id_notice/53');
    $this->json = json_decode($this->_response->getbody());
    $this->assertController('abonne');
    $this->assertAction('avis');
    $this->xpath->assertXPathContentContains($this->json->content,'//p[@class="error"]','Vous devez saisir un titre',$this->_response->getBody());
  }
}



class AbonneControllerAvisNoticeAvisSaveTest extends  AbonneControllerAvisTestCase {
  public function testSaveNewAvis() {
    $expected_avis = Class_AvisNotice::newInstance();
    $expected_avis
      ->setEntete('Sorcellerie')
      ->setAvis('On adore la magie')
      ->setNote(5)
      ->setClefOeuvre('POTTER')
      ->setUser($this->florence)
      ->setAbonOuBib(1)
      ->setStatut(0);
    $expected_avis->save();
    $this->postAndAssertAvisIsSaved($expected_avis);
  }


  public function postAndAssertAvisIsSaved($expected_avis) {
    $data = ['avisEntete' => 'Sorcellerie',
             'avisTexte' => 'On adore la magie',
             'avisNote' => 5,
             'avisSignature' => 'FloCouv'];

    $this->getRequest()
         ->setMethod('POST')
         ->setPost($data);
    $this->dispatch('/opac/abonne/avis/id_notice/53');

    $this->assertEquals('FloCouv', $this->florence->getPseudo());
  }


  public function testSaveExistingAvis() {

    $expected_avis = Class_AvisNotice::newInstanceWithId(12);
    $expected_avis
      ->setEntete('Sorcellerie')
      ->setAvis('On adore la magie')
      ->setNote(5)
      ->setClefOeuvre('POTTER')
      ->setUser($this->florence)
      ->setAbonOuBib(1)
      ->setStatut(0);

    $this->postAndAssertAvisIsSaved($expected_avis);
  }
}


class AbonneControllerAvisNoticeWithAvisTest extends AbonneControllerAvisTestCase {
  protected $_xpath,$_json;
  public function setUp() {
    parent::setUp();

    $avis = $this->fixture('Class_AvisNotice', ['id' => 12,
                                                'Avis' => 'Excellent livre',
                                                'Entete' => 'Le sorcier super mimi',
                                                'note' => 4,
                                                'clef_oeuvre' => 'POTTER',
                                                'user' => $this->florence]);
    $this->dispatch('/opac/abonne/avis/id_notice/53');
    $this->_xpath = new Storm_Test_XPath();
    $this->_json = json_decode($this->_response->getBody());
  }

  public function testAvisFormRendered() {
    $this->assertController('abonne');
    $this->assertAction('avis');
  }


  public function testAvisIsFilled() {
    $this->_xpath->assertXPathContentContains($this->_json->content,'//form//textarea[@name="avisTexte"]','Excellent livre',$this->_response->getBody());
  }

  public function testSignatureIsFloFlo() {
    $this->_xpath->assertXPath($this->_json->content,"//input[@name='avisSignature'][@value='FloFlo']");
  }

  public function testEntete() {
    $this->_xpath->assertXPath($this->_json->content,"//input[@name='avisEntete'][@value='Le sorcier super mimi']");
  }

  public function testNote() {
    $this->_xpath->assertXPath($this->_json->content,"//select[@name='avisNote']/option[@value='4'][@selected='1']");
  }

  public function testFormActionIsAvis() {
    $this->_xpath->assertXPath($this->_json->content,"//form[@action='/abonne/avis/id_notice/53/id/12']");
  }
}


abstract class AvisControllersFixturesTestCase extends AbonneFlorenceIsLoggedControllerTestCase {
  public function setUp() {
    parent::setUp();


    $this->fixture('Class_AdminVar',
                   ['id' => 'MODO_AVIS',
                    'valeur' => 0]);

    $this->fixture('Class_AdminVar',
                   ['id' => 'MODO_AVIS_BIBLIO',
                    'valeur' => 0]);

    $this->fixture('Class_AdminVar',
                   ['id' => 'ID_READ_SPEAKER',
                    'valeur' => '54QCJRHZ31IPBV7GW3DKBPUYYP579A14']);


    $lost=$this->fixture('Class_AvisNotice', ['id' => 178,
                                              'entete' => "Lost highway",
                                              'clef_oeuvre' => 'LOST-LOST',
                                              'avis' => 'Oh po po !',
                                              'note' => 5,
                                              'date_avis' => '2015-03-18 13:00:00',
                                              'user' => $this->florence,
                                              'statut' => 1,
                                              'flags' =>  Class_AvisNotice::ORPHAN_FLAG,
                                              'abon_ou_bib'=>1,
                                              'id_notice' => 30]);

    $this->millenium = $this->fixture('Class_Notice', ['id' => 816,
                                                       'titre_principal' => 'Millenium',
                                                       'clef_alpha' => 'MILLENIUM',
                                                       'auteur_principal' => 'Stieg Larsson',
                                                       'url_vignette' => '',
                                                       'url_image' => ''
                                                       ]
    );





    $this->millenium_with_vignette = $this->fixture('Class_Notice', ['id' => 817,
                                                                     'titre_principal' => 'Millenium',
                                                                     'clef_alpha' => 'MILLENIUM',
                                                                     'auteur_principal' => 'Stieg Larsson',
                                                                     'url_vignette' => 'http://amazon.com/vignette_millenium.png'
                                                                     ]
    );



    $this->avis_millenium =  $this->fixture('Class_AvisNotice', ['id' => 13,
                                                                 'entete' => "J'adore",
                                                                 'avis' => '<div><ul><li>Suspense Intense !</li><li>Suspense Intense !</li></ul></div>',
                                                                 'note' => 5,
                                                                 'date_avis' => '2011-03-18 13:00:00',
                                                                 'user' => $this->florence,
                                                                 'statut' => 0,
                                                                 'abon_ou_bib'=>1 ,
                                                                 'notices' => [$this->millenium,
                                                                               $this->millenium_with_vignette] ]);

    $this->potter = $this->fixture('Class_Notice',
                                   ['id'=> 22,
                                    'titre_principal' => 'Potter et la chambre des secrets',
                                    'auteur_principal' => '',
                                    'url_vignette' => 'http://amazon.com/vignette_potter.png']);

    $this->avis_potter = $this->fixture('Class_AvisNotice',
                                        ['id' => 25,
                                        'entete' => 'Prenant',
                                        'avis' => "Mais un peu trop naïf",
                                        'note'=>4,
                                        'date_avis' => '2010-10-12 10:00:00',
                                        'user'=>$this->florence,
                                        'statut' => 1,
                                        'abon_out_bib' => 1,
                                        'notices' => [$this->potter]]);


    $this->avis_lost = $this->fixture('Class_AvisNotice',
                                        ['id' => 67,
                                         'entete' =>"C'est perdu",
                                         'avis' =>"Plus de notice, ni d'utilisateur",
                                         'note' => 4,
                                         'date_avis' => '2008-10-12 10:00:00',
                                         'user'=>null,
                                         'statut' => 1,
                                         'abon_out_bib' => 1,
                                         'flags' => 1,
                                         'notices' => []]);



    $dupont = $this->fixture('Class_Users', ['id' => 56 ,
                                             'login' => 'Dupont',
                                             'nom'=>'Dupont',
                                             'password' => 'pwd',
                                             'id_abon' => 435]);


    $avis_millenium_dupont = $this->fixture('Class_AvisNotice',
                                            ['id' => 156,
                                             'entete' => "Cool",
                                             'avis' => "effrayant",
                                             'note' => 5,
                                             'setDateAvis' => '2011-03-18 13:00:00',
                                             'auteur' => $dupont,
                                             'user' => $dupont,
                                             'statut' => 0,
                                             'abon_out_bib' => 1,
                                             'notices' =>[$this->millenium]
                                            ]);
    Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
      ->whenCalled('findAllBy')
      ->with(['where' => 'clef_oeuvre like "LOST-%"'])
      ->answers(null)
      ->whenCalled('findAllBy')
      ->with(['where' => 'clef_oeuvre like "MILLENIUM-%"'])
      ->answers([$this->millenium,$this->millenium_with_vignette])
      ;


  }
}


class AbonneControllerAvisBlogControllerViewUnknownAuteurTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/blog/viewauteur/id/999999999');
  }


  public function testPageIsRendered() {
    $this->assertController('blog');
    $this->assertAction('viewauteur');
  }


  public function testDisplayAuteurIntrouvable() {
    $this->assertXPathContentContains('//h1', 'Auteur introuvable');
  }
}




class AbonneControllerAvisViewFicheWithAvisTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/abonne/fiche', true);
  }

  /** @test */
  public function pageShouldDisplayVousAvezRedigeTroiAvis() {
    $this->assertXPathContentContains('//a',
                                      'Vous avez rédigé 3 avis',
                                      $this->_response->getBody());
  }


  /** @test */
  public function linkAvisShouldGoToAbonneViewAvis() {
    $this->assertXPath('//a[contains(@href, "/abonne/viewavis/id/123456")]');
  }


}




class AbonneControllerAvisBlogControllerViewAuteurActionTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();


    $this->dispatch('/opac/blog/viewauteur/id/123456');
  }


  /** @test */
  public function AvisWithoutNoticeShouldBeDisplayedForAuthor() {
    $this->assertXPathContentContains("//div[@class='critique'][1]//a", 'Lost highway');
  }


  public function testPageIsRendered() {
    $this->assertController('blog');
    $this->assertAction('viewauteur');
  }

  public function testMilleniumIsHere() {
    $this->assertXPathContentContains("//div[@class='critique'][2]//h2", 'Millenium (Stieg Larsson)');
  }



  /** @test */
  public function milleniumShouldLinkToNoticeMilleniumWithRetourAbonneViewAvis() {
    $this->assertXPath('//a[contains(@href, "/recherche/viewnotice/id/817/clef/MILLENIUM/retour_abonne/viewavis")]');
  }

  public function testPotterIsHere() {
    $this->assertXPathContentContains("//div[@class='critique'][3]//h2", 'Potter et la chambre des secrets');
  }

  public function testDeleteMilleniumButtonPresent() {
    $this->assertXPath('//a[contains(@href, "/blog/delavisnotice/id/13")]');
  }

  public function testDeletePotterButtonPresent() {
    $this->assertXPath('//a[contains(@href, "/blog/delavisnotice/id/25")]');
  }

  /** @test */
  function rssLinkShouldBePresent() {
    $this->assertXPath("//div[@class='rss']/a[@href='/rss/user/id/123456']");
  }
}




class AbonneControllerAvisViewAvisActionTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/abonne/viewavis/id/123456');
  }


  /** @test */
  public function allAvisContentShouldBeDisplay() {
    $this->assertContains('<p><div><ul><li>Suspense Intense !</li><li>Suspense Intense !</li></ul></div></p>', $this->_response->getBody());
  }


  public function testDeleteMilleniumButtonPresent() {
    $this->assertXPath("//a[contains(@href, '/abonne/delavisnotice/id/13')]");
  }
}




class AbonneControllerAvisBlogControllerViewOtherAuteurAsAdminActionTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->florence->beAdminPortail();
    $this->dispatch('/opac/blog/viewauteur/id/56');
  }


  public function testDeleteMilleniumButtonPresent() {
    $this->assertXPath("//a[contains(@href, '/blog/delavisnotice/id/156')]",$this->_response->getBody());
  }
}



class AbonneControllerAvisRssControllerViewAvisUserTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/rss/user/id/123456');
  }


  /** @test */
  function channelTitleShouldBeAvisDeFloFlo() {
    $this->assertXpathContentContains('//channel/title', 'Avis de FloFlo');
  }


  /** @test */
  function linkShouldBeLocalhostAfiOpac3BlogRssUser123456() {
    $this->assertXPathContentContains('//channel/link',
                                      'http://localhost' . BASE_URL . '/blog/viewauteur/id/123456',
                                      $this->_response->getBody());
  }


  /** @test */
  function channelItemCountShouldBeTwo() {
    $this->assertXpathCount('//channel/item', 2);
  }


  /** @test */
  function firstItemTitleShouldBeJAdore() {
    $this->assertXPathContentContains('//channel/item/title', "J'adore",$this->_response->getBody());
  }


  /** @test */
  function firstItemLinkShouldBeBlogViewAvisId13() {
    $this->assertXPathContentContains('//channel/item/link',
                                      "http://localhost" . BASE_URL . "/blog/viewavis/id/13",$this->_response->getBody());
  }


  /** @test */
  function firstItemPubUpdateShouldBeTue12Oct() {
    $this->assertXPathContentContains('//channel/item/pubDate', "Tue, 12 Oct 2010");
  }


  /** @test */
  function firstItemNoteCritiqueShouldBeImgStars4Gif() {
    $this->assertTrue(false !== strpos($this->_response->getBody(),
                                       'src="http://localhost' . BASE_URL . '/public/admin/images/stars/stars-4.gif"'));
  }
}




class RssControllerViewAvisInexistingUserTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/rss/user/id/-1', true);
  }


  /** @test */
  function linkShouldBeLocalhostAfiOpac3BlogRssUserMinusOne() {
    $this->assertXPathContentContains('//channel/link',
                                      'http://localhost' . BASE_URL . '/blog/viewauteur/id/-1');
  }
}




class AbonneControllerAvisDeleteAvisTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/abonne/delavisnotice/id/25', true);
  }


  public function testRedirectToAbonneAvisPage() {
    $this->assertRedirectTo('/abonne/viewavis/id/123456');
  }
}



class AbonneControllerAvisBlogControllerDeleteAvisTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/blog/delavisnotice/id/25');
  }

  public function testRedirectToViewAuteurPage() {
    $this->assertRedirectTo('/blog/viewauteur/id/123456');
  }
}




class AbonneControllerAvisBlogControllerLastCritiquesTest extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();

    $this->fixture('Class_AdminVar',
                   ['id' => 'MODO_AVIS',
                    'VALEUR' => 1]);

    $this->dispatch('/opac/blog/lastcritique/nb/10', true);
  }


  /** @test */
  public function milleniumRatingShouldBeTheFirst() {
    $this->assertXPathContentContains("//div[@class='critique'][1]//h2", 'Millenium (Stieg Larsson)');
  }


  /** @test */
  public function potterShouldBeTheSecondRating() {
    $this->assertXPathContentContains("//div[@class='critique'][2]//h2", 'Potter et la chambre des secrets', $this->_response->getBody());
  }


  /** @test */
  public function lostRratingShouldNotBeDIsplayed() {
    $this->assertNotXPathContentContains("//div[@class='critique'][3]//h2", 'Oeuvre non trouvée');
  }
}



abstract class ModuleSelectionCritiquesTestCase extends AvisControllersFixturesTestCase {
  public function setUp() {
    parent::setUp();

    $preferences = ['modules' => [3 => ['division' => 2,
                                        'type_module' => 'CRITIQUES',
                                        'preferences' => ['titre' => 'Coups de coeur'
                                        ]
        ]]];
    $profil = $this->fixture('Class_Profil',
                             ['id'=>2,
                              'cfg_modules' => ['blog' =>  ['viewcritiques' => ['nb_display' => 2]]],
                              'cfg_accueil' => $preferences]);

  }
}




class AbonneControllerAvisBlogControllerViewCritiquesTest extends ModuleSelectionCritiquesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/blog/viewcritiques/id_profil/2/id_module/3');
  }

  public function testMilleniumIsHere() {
    $this->assertQueryContentContains('h2', 'Millenium',$this->_response->getBody());
  }

  public function testPotterIsHere() {
    $this->assertQueryContentContains('h2', 'Potter',$this->_response->getBody());
  }

  /** @test */
  public function pagerShouldBeDisplayed() {
    $this->assertXPath('//a[@href="/blog/viewcritiques/id_profil/2/id_module/3/page/2"]', $this->_response->getBody());
  }

  /** @test */
  public function pagerShouldNotContains3Pages() {
    $this->assertNotXPath('//a[contains(@href,"/page/3")]', $this->_response->getBody());
  }


  /** @test */
  public function titleShouldBeCoupsDeCoeur() {
    $this->assertXPathContentContains('//h1', 'Coups de coeur');
  }
}




class AbonneControllerAvisBlogControllerViewCritiquesWithoutModuleTest extends ModuleSelectionCritiquesTestCase {
  /** @test */
  public function titleShouldBeDernieresCritiques() {
    $this->dispatch('/opac/blog/viewcritiques');
    $this->assertXPathContentContains('//h1', 'Dernières critiques');
  }
}




class AbonneControllerAvisRssControllerLastCritiquesTest extends ModuleSelectionCritiquesTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/rss/critiques/id_profil/2/id_module/3', true);
  }


  /** @test */
  public function milleniumShouldBePresent() {
    $this->assertContains('Millenium', $this->_response->getBody());
  }


  /** @test */
  public function potterShouldBePresent() {
    $this->assertContains('Potter', $this->_response->getBody());
  }
}




class AbonneControllerAvisBlogControllerViewReadAvisTest extends  AbonneFlorenceIsLoggedControllerTestCase {
  public function setUp() {
    parent::setUp();

    $millenium = Class_Notice::newInstanceWithId(23,
                                                 ['titre_principal' => 'Millenium (Stieg Larsson)',
                                                  'url_vignette' => 'http://amazon.com/vignette.png',
                                                  'clef_alpha' => 'MILLENIUM']);

    $avis_millenium = Class_AvisNotice::newInstanceWithId(18)
      ->setEntete("J'adore")
      ->setAvis("Suspense intense")
      ->setNote(5)
      ->setDateAvis('2010-03-18 13:00:00')
      ->setUser($this->florence)
      ->setAbonOuBib(1)
      ->setStatut(0)
      ->setNotices([$millenium]);
  }


  /** @test */
  public function blogViewAvisShouldRenderTitleLarsson() {
    $this->dispatch('/opac/blog/viewavis/id/18');

    $this->assertController('blog');
    $this->assertAction('viewavis');

    $this->assertQueryContentContains('div.critique h2', 'Millenium (Stieg Larsson)');
  }



  /** @test */
  public function blogViewAvisWithClefInUrlShouldCleanItInNoticeLink() {
    $this->dispatch('/opac/blog/viewavis/id/18/clef/ZORK');
    $this->assertXPath('//a[contains(@href, "recherche/viewnotice/id/23/clef/MILLENIUM")]',
                       $this->_response->getBody());
  }


  /** @test */
  public function readSpeakerReadAvisShouldRender() {
    $this->dispatch('/opac/blog/readavis/id/18');

    $this->assertController('blog');
    $this->assertAction('readavis');

    $this->assertQueryContentContains('p', 'Millenium (Stieg Larsson)');
    $this->assertQueryContentContains('p', '18 mars 2010');
  }
}



class AbonneControllerEditAvisNoticeNotFoundActionTest extends AbstractControllerTestCase {
  protected $_storm_default_to_volatile = true;
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/abonne/editavisnotice/id/54', true);
  }


  /** @test */
  public function responseShouldReloadPage() {
    xdebug_break();
    $this->assertTrue(false !== strpos(json_decode($this->_response->getBody())->title, 'Sauvegarde en cours'));
  }
}


class AbonneControllerEditAvisNoticeNotAdminLoggedActionTest extends AbstractControllerTestCase {
  protected function _loginHook($account) {
    $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::INVITE;
    $account->ROLE = 'invite';
  }

  public function setUp() {
    parent::setUp();
    $avis = $this->fixture('Class_AvisNotice', ['id' => 54,
                                                'entete' => 'Bonjour !',
                                                'avis' => 'Ceci est le contenu de l\'avis']);
    $this->dispatch('/opac/abonne/editavisnotice/id/54', true);
  }


  /** @test */
  public function responseShouldReloadPage() {
    $this->assertTrue(false !== strpos(json_decode($this->_response->getBody())->title, 'Sauvegarde en cours'),
                      $this->_response->getBody());
  }
}




class AbonneControllerEditAvisNoticeAdminLoggedActionTest extends AbstractControllerTestCase {
  public function setUp() {
    parent::setUp();
    $avis = $this->fixture('Class_AvisNotice', ['id' => 54,
                                                'entete' => 'Bonjour !',
                                                'avis' => 'Ceci est le contenu de l\'avis']);

    $this->dispatch('/opac/abonne/editavisnotice/id/54', true);
    $this->json = json_decode($this->_response->getBody());
    $this->xpath = new Storm_Test_XPath();
  }


  /** @test */
  public function titleShouldBeBonjour() {
    $this->assertEquals('Modifier l\'avis "Bonjour !"', $this->json->title);
  }


  /** @test */
  public function formShouldBePresent() {
    $this->xpath->assertXPath($this->json->content, '//form[contains(@action, "abonne/editavisnotice")]');
  }


  /** @test */
  public function enteteInputShouldBePresent() {
    $this->xpath->assertXPath($this->json->content, '//input[@name="entete"]');
  }


  /** @test */
  public function avisTextareaShouldBePresent() {
    $this->xpath->assertXPath($this->json->content, '//textarea[@name="avis"]');
  }


  /** @test */
  public function urlInputShouldBePresent() {
    $this->xpath->assertXPath($this->json->content, '//input[@name="url"]');
  }


  /** @test */
  public function noteSelectShouldBePresent() {
    $this->xpath->assertNotXPath($this->json->content, '//select[@name="note"]');
  }
}



class AbonneControllerEditAvisNoticeAdminLoggedPostActionTest extends AbstractControllerTestCase {
  public function setUp() {
    parent::setUp();
    $this->fixture('Class_AvisNotice', ['id' => 54,
                                        'entete' => 'Bonjour !',
                                        'avis' => 'Ceci est le contenu de l\'avis',
                                        'note' => 1,
                                        'clef_oeuvre' => 'HUITIEMECOULEURLA-DASTYLE']);

    $this->fixture('Class_Notice', ['id' => 1190178,
                                    'clef_oeuvre' => 'GARCONNIERELA--GREMILLONH-',
                                    'clef_alpha' => 'GARCONNIERELA--GREMILLONH--FLAMMARION-2013-1']);

    $this->postDispatch('/opac/abonne/editavisnotice/id/54',
                        ['entete' => 'bye',
                         'avis' => 'ceci n\'est pas le contenu',
                         'url' => 'http://localhost/recherche/viewnotice/expressionRecherche/la+garconniere/tri/%2A/facette/T1/clef/GARCONNIERELA--GREMILLONH--FLAMMARION-2013-1/id/1190178'],
                        true);

    $this->avis = Class_AvisNotice::find(54);
  }


  /** @test */
  public function enteteShouldBeBye() {
    $this->assertEquals('bye', $this->avis->getEntete());
  }


  /** @test */
  public function avisShouldBeCeciNEstPasLeContenu() {
    $this->assertEquals('ceci n\'est pas le contenu', $this->avis->getAvis());
  }


  /** @test */
  public function cleOeuvreShouldBeGarconniereLa() {
    $this->assertEquals('GARCONNIERELA--GREMILLONH-', $this->avis->getClefOeuvre());
  }
}



class AbonneControllerDeleteAvisNoticeAdminLoggedActionTest extends AbstractControllerTestCase {
  public function setUp() {
    parent::setUp();
    $_SERVER['HTTP_REFERER'] ='opac/recherche/viewnotice/id/1';
    $avis = $this->fixture('Class_AvisNotice', ['id' => 54,
                                                'entete' => 'Bonjour !',
                                                'avis' => 'Ceci est le contenu de l\'avis']);
    $this->dispatch('/opac/abonne/delavisnotice/id/54/expressionRecherche/1', true);
  }


  /** @test */
  public function avisShouldBeRemoved() {
    $this->assertEmpty(Class_AvisNotice::findAll());
  }


  /** @test */
  public function shouldContainsSciptToRedirectToReferer() {
    $this->assertContains('window.location=\"opac\/recherche\/viewnotice\/id\/1\"', $this->_response->getBody());
  }
}

?>