Skip to content
Snippets Groups Projects
ArteVodTest.php 13.3 KiB
Newer Older
pbarroca's avatar
pbarroca committed
<?php
/**
 * Copyright (c) 2022, Agence Française Informatique (AFI). All rights reserved.
Laurent's avatar
Laurent committed
 * BOKEH is free software; you can redistribute it and/or modify
pbarroca's avatar
pbarroca committed
 * 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).
 *
Laurent's avatar
Laurent committed
 * BOKEH is distributed in the hope that it will be useful,
pbarroca's avatar
pbarroca committed
 * 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
Laurent's avatar
Laurent committed
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
require_once 'ArteVodFixtures.php';
abstract class ArteVodHarverstingTestCase extends ModelTestCase {
  protected $_web_client,
    $_service;
  public function setUp() {
Patrick Barroca's avatar
Patrick Barroca committed
    parent::setUp();
    $this->fixture('Class_CosmoVar',
                   ['id' => 'types_docs',
                    'liste' => "1:cd\r\n200:non identifié\r\n201:livres\r\n202:bd\r\nArteVod:Arte VOD"]);
    Class_AdminVar::set('ArteVod_LOGIN', 'user');
    Class_AdminVar::set('ArteVod_SSO_KEY', 'pass');
    $codif_type_doc = $this->fixture('Class_CodifTypeDoc',
                                     ['id' => 'ArteVod',
                                      'famille_id' => Class_CodifTypeDoc::INCONNU,
                                      'bibliotheques' => '1;8',
                                      'annexes' => '10;12',
                                      'sections' => '18;19']);

    $this->fixture('Class_TypeDoc', ['id' => '11',
                                     'codif_type_doc' => $codif_type_doc,
                                     'label'=> 'Type doc']);
efalcy's avatar
efalcy committed

    $this->_web_client = $this->mock();
    ArteVod_Service::setDefaultHttpClient($this->_web_client);
    $command = $this->mock()->whenCalled('execTimedScript')->answers('');
    Class_WebService_BibNumerique_RessourceNumerique::setCommand($command);

    $config = ArteVod_Config::getInstance();
    $config->setTimeSource(new TimeSourceForTest('2023-02-06 10:00:00'));
    $this->_service = new ArteVod_Service($config);
  public function tearDown() {
    ArteVod_Service::setDefaultHttpClient(null);
    parent::tearDown();
Patrick Barroca's avatar
Patrick Barroca committed
  }

class ArteVodHarverstingFourFilmsInTwoPagesTest extends ArteVodHarverstingTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    foreach(['1' => ArteVodFixtures::firstPage(),
             '2' => ArteVodFixtures::secondPage(),
             '3' => ArteVodFixtures::emptyPage()]
            as $page => $answer) {
      $this->_web_client
        ->whenCalled('open_url')
        ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=' . $page . '&q_country=FR')
        ->answers($answer);
    }
    $this->_web_client
      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
      ->beStrict();
    // should delete nothing after harvest
    $this->onLoaderOfModel('Class_Album')
         ->whenCalled('deleteBy')
         ->answers(true);
    $this->_service->harvest();
    $this->edouard = Class_Album::findFirstBy(['order' => 'id']);
    $this->squadra = Class_Album::findFirstBy(['titre'=> 'Squadra Criminale']);
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function shouldHaveCreatedFourAlbums() {
    $this->assertEquals(4, count(Class_Album::findAll()));
Patrick Barroca's avatar
Patrick Barroca committed
  }
llaffont's avatar
llaffont committed


  /** @test */
  public function firstAlbumTitleShouldBeEdouardPoteDroite() {
    $this->assertEquals('Edouard, mon pote de droite',
                        $this->edouard->getTitre());
  }


Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function firstAlbumExternalUriShouldEdouardPoteDroite() {
    $this->assertEquals('https://vod.mediatheque-numerique.com/films/edouard-mon-pote-de-droite',
                        $this->edouard->getExternalUri());
  }

  /** @test */
  public function descriptionShouldContainsCopaindeLycee() {
    $this->assertContains('Edouard est un copain de lycée et a 45 ans',
                          $this->edouard->getDescription());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function yearShouldBe2017() {
    $this->assertEquals('2017', $this->edouard->getAnnee());
  }


  /** @test */
  public function firstAuthorShouldBeTheDirector() {
    $this->assertEquals('Cibien Laurent', $this->edouard->getAuthors()[0]->getName());
    $this->assertEquals('300', $this->edouard->getAuthors()[0]->getResponsibility());
  public function firstActorShouldBePhilippe() {
    $this->assertEquals('Philippe Edouard', $this->edouard->getAuthors()[1]->getName());
    $this->assertEquals('005', $this->edouard->getAuthors()[1]->getResponsibility());
  }


  /** @test */
  public function shouldHavePosterUrl() {
    $this->assertEquals('https://vod.mediatheque-numerique.com/media/59/3e/593e9af9b7428.jpeg',
                        $this->edouard->getPoster());
  }


  /** @test */
  public function fourthAlbumshouldHaveTrailerUrl() {
    $this->assertEquals('https://media.universcine.com/cd/b5/cdb57adc-2a49-11e7-b234-bdefb096dc40.mp4',
                        array_first($this->squadra->getTrailers())->getUrl());
  }


  /** @test */
  public function firstAlbumZone215ShouldBe82Min() {
    $this->assertEquals('82 mn', $this->edouard->getDuration());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function vignetteShouldHaveBeenUploaded() {
    $this->assertTrue(Class_WebService_BibNumerique_RessourceNumerique::getCommand()
                      ->methodHasBeenCalled('execTimedScript'));
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function edouardActorsShouldContainsCibienLaurent() {
    $this->assertContains('Philippe Edouard', $this->edouard->getAuthorsNames());
  }


  /** @test */
  public function edouardTagsShouldContainsPolitique() {
    $expected = 'Politique';
    $this->assertEquals($expected, $this->edouard->getTags());
Patrick Barroca's avatar
Patrick Barroca committed
  }
  public function firstAlbumMatiereShouldContainsDocumentaireAndPolitique() {
    $this->assertEquals('1;2', $this->edouard->getMatiere());
    $this->assertEquals('Documentaire', Class_CodifMatiere::find(1)->getLibelle());
    $this->assertEquals('Politique', Class_CodifMatiere::find(2)->getLibelle());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function bibliothequeShouldBeImportedInByeByeBlondie() {
    $this->assertEquals('1;8', $this->edouard->getBibliotheques());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function annexesShouldBeImportedInByeByeBlondie() {
    $this->assertEquals('10;12', $this->edouard->getAnnexes());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function sectionsShouldBeImportedInByeByeBlondie() {
    $this->assertEquals('18;19', $this->edouard->getSections());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function addingAlreadyExistingAuthorShouldNotCrashAddAuthor() {
    $this->assertNotEquals(null, $this->edouard->addAuthor('Cibien, Laurent', '005'));
Patrick Barroca's avatar
Patrick Barroca committed
  }
class ArteVodHarverstingNewApiUpdateTest extends ArteVodHarverstingTestCase {
  protected $_heroines;
  public function setUp() {
    parent::setUp();
    $this->fixture(Class_CodifMatiere::class,
                   ['id'=>1,
                    'libelle' =>'My added subject',
                    'code_apha' =>'ADDED_SUBJECT'] );

    $this->_album = $this->fixture(Class_Album::class,
                                   ['id' => 20,
                                    'type_doc_id' => 'ArteVod',
                                    'titre' => 'Héros',
                                    'id_origine' => '7040',
                                    'matiere' => '1',
                                    'url_origine' =>'https://mednumv3-backapi.lab.arte.tv/api/v1/',
                                    'fichier'  =>'596525_b88c86285df723b8e5c0f819b132f39f.jpeg',
                                    'external_uri' => 'https://vod.mediatheque-numerique.com/films/heroines']);

    foreach(['1' => ArteVodFixtures::newapiPage(),
             '2' => ArteVodFixtures::emptyPage()]
            as $page => $answer) {

      $this->_web_client
        ->whenCalled('open_url')
        ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=' . $page . '&q_country=FR')
        ->answers($answer);
    }

    $this->_web_client
      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
      ->beStrict();

    // should delete nothing after harvest
    $this->onLoaderOfModel('Class_Album')
         ->whenCalled('deleteBy')
         ->answers(true);

    Class_AdminVar::set('ArteVod_BATCH_UPDATE_NOTICE','1');

    $this->_service->harvest();
    $this->_heroines = Class_Album::find(20);
  }


  /** @test */
  public function shouldHaveUpdatedOneAlbum() {
    $this->assertEquals(1, count(Class_Album::findAll()));
  }


  /** @test */
  public function albumTitleShouldBeHeroines() {
    $this->assertEquals('Héroïnes',
                        $this->_heroines->getTitre());
  }


  /** @test */
  public function yearShouldBeProductionYear() {
    $this->assertEquals('2016',
                        $this->_heroines->getAnnee());
  }


  /** @test */
  public function matieresShouldBeHeroines() {
    $this->assertEquals('1;2;3',
                        $this->_heroines->getMatiere());
  }


  /** @test */
  public function externalUriShouldBeSet() {
    $this->assertEquals('https://vod.mediatheque-numerique.com/films/heroines',
                        $this->_heroines->getExternalUri());
  }


  /**
   * @test
   */
  public function updateShoulBeDeactivated(){
    $this->assertEquals('0', Class_AdminVar::get( 'ArteVod_BATCH_UPDATE_NOTICE'));
  }


  /** @test */
  public function posterShouldBeOnMediasArte() {
    $this->assertEquals('https://mediasarte.mednum.lab.arte.tv/mednum/preprod/medias/thumbs/59/3e/593e6450d4e4e.jpeg',
                        $this->_heroines->getPoster());
  }
}




class ArteVodHarvestingWithoutPasswordTest extends ArteVodHarverstingTestCase {
  public function setUp() {
    parent::setUp();

    Class_AdminVar::set('ArteVod_KEY', '');

    $this->_web_client
      ->whenCalled('open_url')
      ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=1&q_country=FR')
      ->answers(ArteVodFixtures::emptyPage());
    $this->_web_client
      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
      ->beStrict();
    (ArteVod_Service::getInstance())->harvest();
  }


  /** @test */
  public function shouldHaveHarvestedWithoutAuth() {
    $this->assertTrue($this->_web_client->methodHasBeenCalled('open_url'));
  }
}




class ArteVodHarvestingWithoutCountryTest extends ArteVodHarverstingTestCase {
  public function setUp() {
    parent::setUp();

    Class_AdminVar::set('ArteVod_KEY', '');
    Class_AdminVar::set('ArteVod_COUNTRY', ArteVod_Config::COUNTRY_ALL);

    $this->_web_client
      ->whenCalled('open_url')
      ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=1')
      ->answers(ArteVodFixtures::emptyPage());
    $this->_web_client
      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
      ->beStrict();
    (ArteVod_Service::getInstance())->harvest();
  }


  /** @test */
  public function shouldHaveHarvestedWithoutCountry() {
    $this->assertTrue($this->_web_client->methodHasBeenCalled('open_url'));
  }
}




class ArteVodHarverstingEnablingTest extends ArteVodHarverstingTestCase {
  /** @test */
  public function withAllShouldBeEnabled() {
    Class_AdminVar::set('ArteVod_LOGIN', 'user');
    Class_AdminVar::set('ArteVod_SSO_KEY', 'key');
    $this->assertTrue((ArteVod_Config::getInstance())->isEnabled());
  }


  /** @test */
  public function withoutLoginShouldNotBeEnabled() {
    Class_AdminVar::set('ArteVod_LOGIN', '');
    Class_AdminVar::set('ArteVod_SSO_KEY', 'key');
    $this->assertFalse((ArteVod_Config::getInstance())->isEnabled());
  }


  /** @test */
  public function withoutHarvestKeyShouldBeEnabled() {
    Class_AdminVar::set('ArteVod_LOGIN', 'user');
    Class_AdminVar::set('ArteVod_SSO_KEY', 'key');
    Class_AdminVar::set('ArteVod_KEY', '');
    $this->assertTrue((ArteVod_Config::getInstance())->isEnabled());
  }


  /** @test */
  public function withoutSSOKeyShouldNotBeEnabled() {
    Class_AdminVar::set('ArteVod_LOGIN', 'user');
    Class_AdminVar::set('ArteVod_SSO_KEY', '');
    $this->assertFalse((ArteVod_Config::getInstance())->isEnabled());
class ArteVodWebClientTest extends ModelTestCase {
  /** @test */
  public function webClientShouldBeWaitingSimpleWebClient() {
    $this->assertTrue(is_a(ArteVod_Service::getWebClient(),
                           Class_WebService_WaitingSimpleWebClient::class));
  }
}




class ArteVodHarverstingNullContentPageTest extends ArteVodHarverstingTestCase {
  public function setUp() {
    parent::setUp();

    foreach(['1' => ArteVodFixtures::nullContentPage(),
             '2' => ArteVodFixtures::brokenJsonPage(),
             '3' => ArteVodFixtures::emptyPage()]
            as $page => $answer) {

      $this->_web_client
        ->whenCalled('open_url')
        ->with('https://mednumv3-backapi.lab.arte.tv/api/v1/films?page_nb=' . $page . '&q_country=FR')
        ->answers($answer);
    }

    $this->_web_client
      ->whenCalled('setConfig')->with(['timeout' => 10])->answers($this->_web_client)
      ->beStrict();

    $this->_service->harvest();
  }


  /** @test */
  public function shouldHaveDeletedAllAlbum() {
    $this->assertEquals(0, count(Class_Album::findAll()));
  }
}