Skip to content
Snippets Groups Projects
RechercheControllerThumbnailTest.php 7.09 KiB
Newer Older
<?php
/**
 * Copyright (c) 2012-2015, 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
 */


abstract class RechercheControllerThumbnailTestCase extends AbstractControllerTestCase {

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

    Class_Notice_Thumbnail_ProviderAbstract::setDefaultImageFactory($this->_getCoolImagick());
  }


  public function tearDown() {
    Class_Notice_Thumbnail_ProviderAbstract::setDefaultImageFactory(null);
    parent::tearDown();
  }


  protected function _getCoolImagick() {
    return $this->mock()
                ->whenCalled('newImage')
                ->answers($this->mock()
                          ->whenCalled('thumbnailImage')
                          ->answers(true)
                          ->whenCalled('writeImage')
                          ->answers(true));
  }
}




class RechercheControllerThumbnailCmsTest
  extends RechercheControllerThumbnailTestCase {

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

    Class_Article::setFileWriter($this->mock()
                                 ->whenCalled('fileExists')
                                 ->answers(true));

    $article = $this->fixture(Class_Article::class,
                              ['id' => 123,
                               'titre' => 'An article with img',
                               'contenu' => 'This article should contains an image like this <img src="/images/articles/img.png" />']);
    $this->dispatch('recherche/vignette/id_notice/1');
  }

  public function tearDown() {
    Class_Article::setFileWriter(null);
    parent::tearDown();
  }


  /** @test */
  public function responseShouldRedirectToVignetteUrl() {
    $this->assertContains('/temp/vignettes_titre/ANARTICLEWITHIMG-123-----8.jpg',
                          $this->getResponseLocation());
  }
}




class RechercheControllerThumbnailAlbumCmsWithImageTest
  extends RechercheControllerThumbnailTestCase {

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

    $new_image = $this
      ->mock()
      ->whenCalled('thumbnailImage')->answers(true)
      ->whenCalled('writeImage')->answers(true);

    $imagick = $this
      ->mock()

      ->whenCalled('newImage')->with('./userfiles/temp/765451650634531fa7f2389da916b8bb.png')
      ->answers(null)

      ->whenCalled('newImage')->with('./userfiles/album/777/big/media/12.png')
      ->answers($new_image)

      ->beStrict();

    Class_Notice_Thumbnail_ProviderAbstract::setDefaultImageFactory($imagick);

    $album = $this->fixture(Class_Album::class,
                            ['id' => 777,
                             'titre' => 'Testing Album',
                             'visible' => 1,
                             'status' => Class_Album::STATUS_VALIDATED,
                             'ressources' => [$this->fixture(Class_AlbumRessource::class,
                                                             ['id' => 12,
                                                              'fichier' => '12.png'])]]);
    $album->index();

    $this->dispatch('recherche/vignette/id_notice/1');
  }


  /** @test */
  public function shouldRedirectToGeneratedThumbnailUrl() {
    $this->assertContains('/temp/vignettes_titre/TESTINGALBUM------101.jpg',
                          $this->getResponseLocation());
  }
}



class RechercheControllerThumbnailAlbumSiteWithImageTest
  extends RechercheControllerThumbnailTestCase {

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

    $album = $this->fixture(Class_Album::class,
                            ['id' => 777,
                             'titre' => 'Testing Album',
                             'visible' => 1,
                             'status' => Class_Album::STATUS_VALIDATED,
                             'ressources' => [$this->fixture(Class_AlbumRessource::class,
                                                              'poster' => 'linuxfr.jpg',
                                                              'url' => 'http://www.linuxfr.org',
                                                              'fichier' => '12.png'])]]);
    $album->index();

    $this->dispatch('recherche/vignette/id_notice/1');
  }


  /** @test */
  public function shouldRedirectToGeneratedThumbnailUrl() {
    $this->assertContains('/temp/vignettes_titre/TESTINGALBUM------101.jpg',
                          $this->getResponseLocation());
  }
}
class RechercheControllerThumbnailAlbumFromDigitalResourceWithBrokenThumbnailTest
  extends RechercheControllerThumbnailTestCase {

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

    $fs = $this->mock()
               ->whenCalled('file_exists')
               ->answers(false)

               ->whenCalled('unlink')
               ->answers(false);

    Class_Notice_Thumbnail_ProviderAlbum::setFileSystem($fs);
    Class_Album::setFileSystem($fs);

    Class_WebService_Abstract::setHttpClient($this->mock()

                                             ->whenCalled('getResponse')
                                             ->with('http://jamendo.com/images/medium_XXXX.jpg')
                                             ->answers(new Class_Testing_HttpResponse(['Body' => 'a body']))

                                             ->beStrict());

    $album = $this->fixture(Class_Album::class,
                            ['id' => 777,
                             'titre' => 'Testing Album',
                             'visible' => 1,
                             'type_doc_id' => Class_TypeDoc::JAMENDO,
                             'fichier' => 'something_deleted.jpg',
                             'status' => Class_Album::STATUS_VALIDATED]);

    $album
      ->addPosterURI('http://jamendo.com/images/medium_XXXX.jpg')
      ->assertSave();

    $album->index();
    $album->getNotice()
          ->setUrlVignette('')
          ->setUrlImage('')
          ->assertSave();

    $this->dispatch('recherche/vignette/id_notice/' . $album->getNotice()->getId());
  }


  public function tearDown() {
    Class_Notice_Thumbnail_ProviderAlbum::setFileSystem(null);
    Class_Album::setFileSystem(null);
    Class_WebService_Abstract::resetHttpClient();
    parent::tearDown();
  }


  /** @test */
  public function shouldRedirectToGeneratedThumbnailUrl() {
    $this->assertContains('/temp/vignettes_titre/TESTINGALBUM------115.jpg',
                          $this->getResponseLocation());
  }
}