server/src/tests/Controllers/ThemeControllerTest.php
changeset 322 084aae09edf4
parent 306 3fccf43160a7
child 387 7fba86fa8604
equal deleted inserted replaced
321:aefaad270b9b 322:084aae09edf4
     8 /**
     8 /**
     9  *
     9  *
    10  */
    10  */
    11 class ThemeControllerTest extends TestCase {
    11 class ThemeControllerTest extends TestCase {
    12 
    12 
    13     private $sparqlClient;
       
    14 
       
    15     public function setUp() {
    13     public function setUp() {
    16 
    14 
    17         parent::setup();
    15         parent::setup();
    18 
    16 
    19         // create a mock of the post repository interface and inject it into the
       
    20         // IoC container
       
    21         $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient');
       
    22         $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient);
       
    23     }
    17     }
    24 
    18 
    25     public function tearDown() {
    19     public function tearDown() {
    26         m::close();
    20         m::close();
    27         parent::tearDown();
    21         parent::tearDown();
    28     }
    22     }
    29 
    23 
    30     public function testIndexQuery() {
    24     public function testIndex() {
    31 
    25 
    32         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
    26         $query = [
    33                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
    27             'index' => env('ELASTICSEARCH_INDEX'),
    34                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
    28             'body' => [
    35                     FILTER (isIRI(?o) && regex(str(?o), '^".config('corpusparole.bnf_ark_base_url')."')) .
    29                 'size' => 0,
    36                 }
    30                 'aggs' => [
    37                 GROUP BY ?o
    31                     "subjects" => [
    38                 ORDER BY DESC(?count)");
    32                         "nested" => [ "path" => "subject" ],
    39 
    33                         "aggs" => [
    40         $this->sparqlClient
    34                             "subjects" => [
    41             ->shouldReceive('query')
    35                                 "terms" => [
       
    36                                     "field" => "subject.label_code",
       
    37                                     "size" => config('corpusparole.theme_default_limit'),
       
    38                                     "order" => [ '_count' => 'desc' ],
       
    39                                     "include" => ".*\|bnf\|.*"
       
    40                                 ]
       
    41                             ]
       
    42                         ]
       
    43                     ]
       
    44                 ]
       
    45             ]
       
    46         ];
       
    47 
       
    48         Es::shouldReceive('search')
       
    49             ->once()
    42             ->with($query)
    50             ->with($query)
    43             ->once()
    51             ->andReturn([
    44             ->andReturn(new \ArrayIterator([]));
    52                 "took" => 27,
    45         $this->get('/api/v1/stats/themes/');
    53                 "timed_out" => false,
    46     }
    54                 "_shards" => [
    47 
    55                     "total" => 1,
    48     public function testIndexQueryBnf() {
    56                     "successful" => 1,
    49 
    57                     "failed" => 0
    50         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
    58                 ],
    51                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
    59                 "hits" => [
    52                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
    60                     "total" => 3011,
    53                     FILTER (isIRI(?o) && regex(str(?o), '^".config('corpusparole.bnf_ark_base_url')."')) .
    61                     "max_score" => 0.0,
    54                 }
    62                     "hits" => [ ]
    55                 GROUP BY ?o
    63                 ],
    56                 ORDER BY DESC(?count)");
    64                 "aggregations" => [
    57 
    65                     "subjects" => [
    58         $this->sparqlClient
    66                         "doc_count" => 41524,
    59             ->shouldReceive('query')
    67                         "subjects" => [
    60             ->with($query)
    68                             "doc_count_error_upper_bound" => 0,
    61             ->once()
    69                             "sum_other_doc_count" => 13678,
    62             ->andReturn(new \ArrayIterator([]));
    70                             "buckets" => [ [
    63         $this->get('/api/v1/stats/themes/?filter=bnf');
    71                                 "key" => "professions|bnf|ark:/12148/cb13318415c",
    64     }
    72                                 "doc_count" => 1412
    65 
    73                             ], [
    66 
    74                                 "key" => "travail non rémunéré|bnf|ark:/12148/cb16604691s",
    67     public function testIndexQueryAll() {
    75                                 "doc_count" => 1092
    68 
    76                             ], [
    69         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
    77                                 "key" => "famille|bnf|ark:/12148/cb119339867",
    70                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
    78                                 "doc_count" => 1050
    71                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
    79                             ], [
    72                 }
    80                                 "key" => "oiseaux|bnf|ark:/12148/cb11932889r",
    73                 GROUP BY ?o
    81                                 "doc_count" => 1003
    74                 ORDER BY DESC(?count)");
    82                             ], [
    75 
    83                                 "key" => "météorologie|bnf|ark:/12148/cb11932496x",
    76         $this->sparqlClient
    84                                 "doc_count" => 1001
    77             ->shouldReceive('query')
    85                             ], [
    78             ->with($query)
    86                                 "key" => "plantes|bnf|ark:/12148/cb11933145f",
    79             ->once()
    87                                 "doc_count" => 995
    80             ->andReturn(new \ArrayIterator([]));
    88                             ], [
    81         $this->get('/api/v1/stats/themes/?filter=all');
    89                                 "key" => "animaux sauvages|bnf|ark:/12148/cb11930908q",
    82     }
    90                                 "doc_count" => 989
    83 
    91                             ], [
    84 
    92                                 "key" => "arbres|bnf|ark:/12148/cb11934786x",
    85     public function testIndexQueryNone() {
    93                                 "doc_count" => 989
    86 
    94                             ] ]
    87         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
    95                         ]
    88                     ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
    96                     ]
    89                     ?s <http://purl.org/dc/elements/1.1/subject> ?o .
    97                 ]
    90                 }
    98             ]);
    91                 GROUP BY ?o
       
    92                 ORDER BY DESC(?count)");
       
    93 
       
    94         $this->sparqlClient
       
    95             ->shouldReceive('query')
       
    96             ->with($query)
       
    97             ->once()
       
    98             ->andReturn(new \ArrayIterator([]));
       
    99         $this->get('/api/v1/stats/themes/?filter=none');
       
   100     }
       
   101 
       
   102 
       
   103     public function testIndexQueryEmpty() {
       
   104 
       
   105         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
   106                 ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
   107                 ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
   108             }
       
   109             GROUP BY ?o
       
   110             ORDER BY DESC(?count)");
       
   111 
       
   112         $this->sparqlClient
       
   113             ->shouldReceive('query')
       
   114             ->with($query)
       
   115             ->once()
       
   116             ->andReturn(new \ArrayIterator([]));
       
   117         $this->get('/api/v1/stats/themes/?filter=');
       
   118     }
       
   119 
       
   120     public function testIndexQueryUri() {
       
   121 
       
   122         $query = preg_replace('/\s+/', ' ', "select (?o as ?theme) (COUNT(?s) as ?count) where {
       
   123                 ?s a <http://www.europeana.eu/schemas/edm/ProvidedCHO> .
       
   124                 ?s <http://purl.org/dc/elements/1.1/subject> ?o .
       
   125                 FILTER isIRI(?o) .
       
   126             }
       
   127             GROUP BY ?o
       
   128             ORDER BY DESC(?count)");
       
   129 
       
   130         $this->sparqlClient
       
   131             ->shouldReceive('query')
       
   132             ->with($query)
       
   133             ->once()
       
   134             ->andReturn(new \ArrayIterator([]));
       
   135         $this->get('/api/v1/stats/themes/?filter=uri');
       
   136     }
       
   137 
       
   138 
       
   139     public function testIndex() {
       
   140 
       
   141         $this->sparqlClient
       
   142             ->shouldReceive('query')
       
   143             ->once()
       
   144             ->andReturn(new \ArrayIterator([
       
   145                 (object)['theme'=>new Resource('http://lexvo.org/id/iso639-3/gsw'), 'count' => Literal::create(44)],
       
   146                 (object)['theme'=>new Resource('http://ark.bnf.fr/ark:/12148/cb119339867'), 'count' => Literal::create(33)],
       
   147                 (object)['theme'=>Literal::create('Français', 'fr'), 'count' => Literal::create(22)],
       
   148             ]));
       
   149         $this->get('/api/v1/stats/themes/')->assertTrue($this->response->isOk(), $this->response->content());
    99         $this->get('/api/v1/stats/themes/')->assertTrue($this->response->isOk(), $this->response->content());
   150         $this->seeJsonEquals(["themes" => [
   100         $this->seeJsonEquals(["themes" => [
   151             "http://lexvo.org/id/iso639-3/gsw" => ["label" => "alémanique", "count" => 44],
   101             "http://ark.bnf.fr/ark:/12148/cb13318415c" => ['label' => 'professions', 'count' => 1412 ],
   152             "http://ark.bnf.fr/ark:/12148/cb119339867" => ["label" => "famille", "count" => 33],
   102             "http://ark.bnf.fr/ark:/12148/cb16604691s" => ['label' => 'travail non rémunéré', 'count' => 1092 ],
   153             "Français" => ["label" => "Français", "count" => 22],
   103             "http://ark.bnf.fr/ark:/12148/cb119339867" => ['label' => 'famille', 'count' => 1050 ],
       
   104             "http://ark.bnf.fr/ark:/12148/cb11932889r" => ['label' => 'oiseaux', 'count' => 1003 ],
       
   105             "http://ark.bnf.fr/ark:/12148/cb11932496x" => ['label' => 'météorologie', 'count' => 1001 ],
       
   106             "http://ark.bnf.fr/ark:/12148/cb11933145f" => ['label' => 'plantes', 'count' => 995  ],
       
   107             "http://ark.bnf.fr/ark:/12148/cb11930908q" => ['label' => 'animaux sauvages', 'count' => 989 ],
       
   108             "http://ark.bnf.fr/ark:/12148/cb11934786x" => ['label' => 'arbres', 'count' => 989 ]
   154         ]]);
   109         ]]);
   155     }
   110 
       
   111     }
       
   112 
       
   113     public function testIndexAll() {
       
   114 
       
   115         $query = [
       
   116             'index' => env('ELASTICSEARCH_INDEX'),
       
   117             'body' => [
       
   118                 'size' => 0,
       
   119                 'aggs' => [
       
   120                     "subjects" => [
       
   121                         "nested" => [ "path" => "subject" ],
       
   122                         "aggs" => [
       
   123                             "subjects" => [
       
   124                                 "terms" => [
       
   125                                     "field" => "subject.label_code",
       
   126                                     "size" => config('corpusparole.theme_default_limit'),
       
   127                                     "order" => [ '_count' => 'desc' ]
       
   128                                 ]
       
   129                             ]
       
   130                         ]
       
   131                     ]
       
   132                 ]
       
   133             ]
       
   134         ];
       
   135 
       
   136         Es::shouldReceive('search')
       
   137             ->once()
       
   138             ->with($query)
       
   139             ->andReturn([
       
   140                 "took" => 27,
       
   141                 "timed_out" => false,
       
   142                 "_shards" => [
       
   143                     "total" => 1,
       
   144                     "successful" => 1,
       
   145                     "failed" => 0
       
   146                 ],
       
   147                 "hits" => [
       
   148                     "total" => 3011,
       
   149                     "max_score" => 0.0,
       
   150                     "hits" => [ ]
       
   151                 ],
       
   152                 "aggregations" => [
       
   153                     "subjects" => [
       
   154                         "doc_count" => 41524,
       
   155                         "subjects" => [
       
   156                             "doc_count_error_upper_bound" => 0,
       
   157                             "sum_other_doc_count" => 13678,
       
   158                             "buckets" => [ [
       
   159                                 "key" => "professions|bnf|ark:/12148/cb13318415c",
       
   160                                 "doc_count" => 1412
       
   161                             ], [
       
   162                                 "key" => "travail non rémunéré|bnf|ark:/12148/cb16604691s",
       
   163                                 "doc_count" => 1092
       
   164                             ], [
       
   165                                 "key" => "famille|bnf|ark:/12148/cb119339867",
       
   166                                 "doc_count" => 1050
       
   167                             ], [
       
   168                                 "key" => "oiseaux|bnf|ark:/12148/cb11932889r",
       
   169                                 "doc_count" => 1003
       
   170                             ], [
       
   171                                 "key" => "Français|lxv|fra",
       
   172                                 "doc_count" => 1002
       
   173                             ], [
       
   174                                 "key" => "météorologie|bnf|ark:/12148/cb11932496x",
       
   175                                 "doc_count" => 1001
       
   176                             ], [
       
   177                                 "key" => "plantes|bnf|ark:/12148/cb11933145f",
       
   178                                 "doc_count" => 995
       
   179                             ], [
       
   180                                 "key" => "Breton|lxv|bzh",
       
   181                                 "doc_count" => 992
       
   182                             ], [
       
   183                                 "key" => "animaux sauvages|bnf|ark:/12148/cb11930908q",
       
   184                                 "doc_count" => 989
       
   185                             ], [
       
   186                                 "key" => "arbres|bnf|ark:/12148/cb11934786x",
       
   187                                 "doc_count" => 989
       
   188                             ] ]
       
   189                         ]
       
   190                     ]
       
   191                 ]
       
   192             ]);
       
   193         $this->get('/api/v1/stats/themes/?filter=all')->assertTrue($this->response->isOk(), $this->response->content());
       
   194         $this->seeJsonEquals(["themes" => [
       
   195             "http://ark.bnf.fr/ark:/12148/cb13318415c" => ['label' => 'professions', 'count' => 1412 ],
       
   196             "http://ark.bnf.fr/ark:/12148/cb16604691s" => ['label' => 'travail non rémunéré', 'count' => 1092 ],
       
   197             "http://ark.bnf.fr/ark:/12148/cb119339867" => ['label' => 'famille', 'count' => 1050 ],
       
   198             "http://ark.bnf.fr/ark:/12148/cb11932889r" => ['label' => 'oiseaux', 'count' => 1003 ],
       
   199             "http://lexvo.org/id/iso639-3/fra" => ['label' => 'Français', 'count' => 1002 ],
       
   200             "http://ark.bnf.fr/ark:/12148/cb11932496x" => ['label' => 'météorologie', 'count' => 1001 ],
       
   201             "http://ark.bnf.fr/ark:/12148/cb11933145f" => ['label' => 'plantes', 'count' => 995  ],
       
   202             "http://lexvo.org/id/iso639-3/bzh" => ['label' => 'Breton', 'count' => 992 ],
       
   203             "http://ark.bnf.fr/ark:/12148/cb11930908q" => ['label' => 'animaux sauvages', 'count' => 989 ],
       
   204             "http://ark.bnf.fr/ark:/12148/cb11934786x" => ['label' => 'arbres', 'count' => 989 ]
       
   205         ]]);
       
   206 
       
   207     }
       
   208 
       
   209     public function testIndexLexvo() {
       
   210 
       
   211         $query = [
       
   212             'index' => env('ELASTICSEARCH_INDEX'),
       
   213             'body' => [
       
   214                 'size' => 0,
       
   215                 'aggs' => [
       
   216                     "subjects" => [
       
   217                         "nested" => [ "path" => "subject" ],
       
   218                         "aggs" => [
       
   219                             "subjects" => [
       
   220                                 "terms" => [
       
   221                                     "field" => "subject.label_code",
       
   222                                     "size" => config('corpusparole.theme_default_limit'),
       
   223                                     "order" => [ '_count' => 'desc' ],
       
   224                                     "include" => ".*\|lxv\|.*"
       
   225                                 ]
       
   226                             ]
       
   227                         ]
       
   228                     ]
       
   229                 ]
       
   230             ]
       
   231         ];
       
   232 
       
   233         Es::shouldReceive('search')
       
   234             ->once()
       
   235             ->with($query)
       
   236             ->andReturn([
       
   237                 "took" => 27,
       
   238                 "timed_out" => false,
       
   239                 "_shards" => [
       
   240                     "total" => 1,
       
   241                     "successful" => 1,
       
   242                     "failed" => 0
       
   243                 ],
       
   244                 "hits" => [
       
   245                     "total" => 3011,
       
   246                     "max_score" => 0.0,
       
   247                     "hits" => [ ]
       
   248                 ],
       
   249                 "aggregations" => [
       
   250                     "subjects" => [
       
   251                         "doc_count" => 41524,
       
   252                         "subjects" => [
       
   253                             "doc_count_error_upper_bound" => 0,
       
   254                             "sum_other_doc_count" => 13678,
       
   255                             "buckets" => [ [
       
   256                                 "key" => "Français|lxv|fra",
       
   257                                 "doc_count" => 1002
       
   258                             ], [
       
   259                                 "key" => "Breton|lxv|bzh",
       
   260                                 "doc_count" => 992
       
   261                             ] ]
       
   262                         ]
       
   263                     ]
       
   264                 ]
       
   265             ]);
       
   266         $this->get('/api/v1/stats/themes/?filter=lexvo')->assertTrue($this->response->isOk(), $this->response->content());
       
   267         $this->seeJsonEquals(["themes" => [
       
   268             "http://lexvo.org/id/iso639-3/fra" => ['label' => 'Français', 'count' => 1002 ],
       
   269             "http://lexvo.org/id/iso639-3/bzh" => ['label' => 'Breton', 'count' => 992 ]
       
   270         ]]);
       
   271 
       
   272     }
       
   273 
       
   274     private function getOrderQuery($order) {
       
   275         return [
       
   276             'index' => env('ELASTICSEARCH_INDEX'),
       
   277             'body' => [
       
   278                 'size' => 0,
       
   279                 'aggs' => [
       
   280                     "subjects" => [
       
   281                         "nested" => [ "path" => "subject" ],
       
   282                         "aggs" => [
       
   283                             "subjects" => [
       
   284                                 "terms" => [
       
   285                                     "field" => "subject.label_code",
       
   286                                     "size" => config('corpusparole.theme_default_limit'),
       
   287                                     "order" => $order,
       
   288                                     "include" => ".*\|bnf\|.*"
       
   289                                 ]
       
   290                             ]
       
   291                         ]
       
   292                     ]
       
   293                 ]
       
   294             ]
       
   295         ];
       
   296 
       
   297     }
       
   298 
       
   299     private function getRespSort() {
       
   300         return [
       
   301             "took" => 27,
       
   302             "timed_out" => false,
       
   303             "_shards" => [
       
   304                 "total" => 1,
       
   305                 "successful" => 1,
       
   306                 "failed" => 0
       
   307             ],
       
   308             "hits" => [
       
   309                 "total" => 3011,
       
   310                 "max_score" => 0.0,
       
   311                 "hits" => [ ]
       
   312             ],
       
   313             "aggregations" => [
       
   314                 "subjects" => [
       
   315                     "doc_count" => 41524,
       
   316                     "subjects" => [
       
   317                         "doc_count_error_upper_bound" => 0,
       
   318                         "sum_other_doc_count" => 13678,
       
   319                         "buckets" => [  ]
       
   320                     ]
       
   321                 ]
       
   322             ]
       
   323         ];
       
   324     }
       
   325 
       
   326     public function testIndexSort() {
       
   327 
       
   328         Es::shouldReceive('search')
       
   329             ->once()
       
   330             ->with($this->getOrderQuery(['_count' => 'desc']))
       
   331             ->andReturn($this->getRespSort());
       
   332         $this->get('/api/v1/stats/themes/')->assertTrue($this->response->isOk(), $this->response->content());
       
   333 
       
   334     }
       
   335 
       
   336     public function testIndexSortReverseCount() {
       
   337         Es::shouldReceive('search')
       
   338             ->once()
       
   339             ->with($this->getOrderQuery(['_count' => 'asc']))
       
   340             ->andReturn($this->getRespSort());
       
   341         $this->get('/api/v1/stats/themes/?sort=-count')->assertTrue($this->response->isOk(), $this->response->content());
       
   342     }
       
   343 
       
   344     public function testIndexSortLabel() {
       
   345         Es::shouldReceive('search')
       
   346             ->once()
       
   347             ->with($this->getOrderQuery(['_term' => 'asc']))
       
   348             ->andReturn($this->getRespSort());
       
   349         $this->get('/api/v1/stats/themes/?sort=label')->assertTrue($this->response->isOk(), $this->response->content());
       
   350     }
       
   351 
       
   352     public function testIndexSortAlphabetical() {
       
   353         Es::shouldReceive('search')
       
   354             ->once()
       
   355             ->with($this->getOrderQuery(['_term' => 'asc']))
       
   356             ->andReturn($this->getRespSort());
       
   357         $this->get('/api/v1/stats/themes/?sort=alphabetical')->assertTrue($this->response->isOk(), $this->response->content());
       
   358     }
       
   359 
       
   360     public function testIndexSortLabelReverse() {
       
   361         Es::shouldReceive('search')
       
   362             ->once()
       
   363             ->with($this->getOrderQuery(['_term' => 'desc']))
       
   364             ->andReturn($this->getRespSort());
       
   365         $this->get('/api/v1/stats/themes/?sort=-label')->assertTrue($this->response->isOk(), $this->response->content());
       
   366     }
       
   367 
       
   368 
       
   369     public function testIndexLimit() {
       
   370 
       
   371         $query = [
       
   372             'index' => env('ELASTICSEARCH_INDEX'),
       
   373             'body' => [
       
   374                 'size' => 0,
       
   375                 'aggs' => [
       
   376                     "subjects" => [
       
   377                         "nested" => [ "path" => "subject" ],
       
   378                         "aggs" => [
       
   379                             "subjects" => [
       
   380                                 "terms" => [
       
   381                                     "field" => "subject.label_code",
       
   382                                     "size" => 3,
       
   383                                     "order" => [ '_count' => 'desc' ],
       
   384                                     "include" => ".*\|bnf\|.*"
       
   385                                 ]
       
   386                             ]
       
   387                         ]
       
   388                     ]
       
   389                 ]
       
   390             ]
       
   391         ];
       
   392 
       
   393         // This is not normally what should be received, but we test that the limit is respected
       
   394         Es::shouldReceive('search')
       
   395             ->once()
       
   396             ->with($query)
       
   397             ->andReturn([
       
   398                 "took" => 27,
       
   399                 "timed_out" => false,
       
   400                 "_shards" => [
       
   401                     "total" => 1,
       
   402                     "successful" => 1,
       
   403                     "failed" => 0
       
   404                 ],
       
   405                 "hits" => [
       
   406                     "total" => 3011,
       
   407                     "max_score" => 0.0,
       
   408                     "hits" => [ ]
       
   409                 ],
       
   410                 "aggregations" => [
       
   411                     "subjects" => [
       
   412                         "doc_count" => 41524,
       
   413                         "subjects" => [
       
   414                             "doc_count_error_upper_bound" => 0,
       
   415                             "sum_other_doc_count" => 13678,
       
   416                             "buckets" => [ [
       
   417                                 "key" => "professions|bnf|ark:/12148/cb13318415c",
       
   418                                 "doc_count" => 1412
       
   419                             ], [
       
   420                                 "key" => "travail non rémunéré|bnf|ark:/12148/cb16604691s",
       
   421                                 "doc_count" => 1092
       
   422                             ], [
       
   423                                 "key" => "famille|bnf|ark:/12148/cb119339867",
       
   424                                 "doc_count" => 1050
       
   425                             ], [
       
   426                                 "key" => "oiseaux|bnf|ark:/12148/cb11932889r",
       
   427                                 "doc_count" => 1003
       
   428                             ], [
       
   429                                 "key" => "météorologie|bnf|ark:/12148/cb11932496x",
       
   430                                 "doc_count" => 1001
       
   431                             ], [
       
   432                                 "key" => "plantes|bnf|ark:/12148/cb11933145f",
       
   433                                 "doc_count" => 995
       
   434                             ], [
       
   435                                 "key" => "animaux sauvages|bnf|ark:/12148/cb11930908q",
       
   436                                 "doc_count" => 989
       
   437                             ], [
       
   438                                 "key" => "arbres|bnf|ark:/12148/cb11934786x",
       
   439                                 "doc_count" => 989
       
   440                             ] ]
       
   441                         ]
       
   442                     ]
       
   443                 ]
       
   444             ]);
       
   445         $this->get('/api/v1/stats/themes/?limit=3')->assertTrue($this->response->isOk(), $this->response->content());
       
   446         $this->seeJsonEquals(["themes" => [
       
   447             "http://ark.bnf.fr/ark:/12148/cb13318415c" => ['label' => 'professions', 'count' => 1412 ],
       
   448             "http://ark.bnf.fr/ark:/12148/cb16604691s" => ['label' => 'travail non rémunéré', 'count' => 1092 ],
       
   449             "http://ark.bnf.fr/ark:/12148/cb119339867" => ['label' => 'famille', 'count' => 1050 ]
       
   450         ]]);
       
   451 
       
   452     }
       
   453 
       
   454     public function testIndexIndex() {
       
   455 
       
   456         $query = [
       
   457             'index' => env('ELASTICSEARCH_INDEX'),
       
   458             'body' => [
       
   459                 'size' => 0,
       
   460                 'aggs' => [
       
   461                     "subjects" => [
       
   462                         "nested" => [ "path" => "subject" ],
       
   463                         "aggs" => [
       
   464                             "subjects" => [
       
   465                                 "terms" => [
       
   466                                     "field" => "subject.label_code",
       
   467                                     "size" => 6,
       
   468                                     "order" => [ '_count' => 'desc' ],
       
   469                                     "include" => ".*\|bnf\|.*"
       
   470                                 ]
       
   471                             ]
       
   472                         ]
       
   473                     ]
       
   474                 ]
       
   475             ]
       
   476         ];
       
   477 
       
   478         // This is not normally what should be received, but we test that the limit is respected
       
   479         Es::shouldReceive('search')
       
   480             ->once()
       
   481             ->with($query)
       
   482             ->andReturn([
       
   483                 "took" => 27,
       
   484                 "timed_out" => false,
       
   485                 "_shards" => [
       
   486                     "total" => 1,
       
   487                     "successful" => 1,
       
   488                     "failed" => 0
       
   489                 ],
       
   490                 "hits" => [
       
   491                     "total" => 3011,
       
   492                     "max_score" => 0.0,
       
   493                     "hits" => [ ]
       
   494                 ],
       
   495                 "aggregations" => [
       
   496                     "subjects" => [
       
   497                         "doc_count" => 41524,
       
   498                         "subjects" => [
       
   499                             "doc_count_error_upper_bound" => 0,
       
   500                             "sum_other_doc_count" => 13678,
       
   501                             "buckets" => [ [
       
   502                                 "key" => "professions|bnf|ark:/12148/cb13318415c",
       
   503                                 "doc_count" => 1412
       
   504                             ], [
       
   505                                 "key" => "travail non rémunéré|bnf|ark:/12148/cb16604691s",
       
   506                                 "doc_count" => 1092
       
   507                             ], [
       
   508                                 "key" => "famille|bnf|ark:/12148/cb119339867",
       
   509                                 "doc_count" => 1050
       
   510                             ], [
       
   511                                 "key" => "oiseaux|bnf|ark:/12148/cb11932889r",
       
   512                                 "doc_count" => 1003
       
   513                             ], [
       
   514                                 "key" => "météorologie|bnf|ark:/12148/cb11932496x",
       
   515                                 "doc_count" => 1001
       
   516                             ], [
       
   517                                 "key" => "plantes|bnf|ark:/12148/cb11933145f",
       
   518                                 "doc_count" => 995
       
   519                             ], [
       
   520                                 "key" => "animaux sauvages|bnf|ark:/12148/cb11930908q",
       
   521                                 "doc_count" => 989
       
   522                             ], [
       
   523                                 "key" => "arbres|bnf|ark:/12148/cb11934786x",
       
   524                                 "doc_count" => 989
       
   525                             ] ]
       
   526                         ]
       
   527                     ]
       
   528                 ]
       
   529             ]);
       
   530         $this->get('/api/v1/stats/themes/?limit=3&index=1')->assertTrue($this->response->isOk(), $this->response->content());
       
   531         $this->seeJsonEquals(["themes" => [
       
   532             "http://ark.bnf.fr/ark:/12148/cb11932889r" => ['label' => 'oiseaux', 'count' => 1003 ],
       
   533             "http://ark.bnf.fr/ark:/12148/cb11932496x" => ['label' => 'météorologie', 'count' => 1001 ],
       
   534             "http://ark.bnf.fr/ark:/12148/cb11933145f" => ['label' => 'plantes', 'count' => 995  ]
       
   535         ]]);
       
   536 
       
   537     }
       
   538 
       
   539     public function testIndexLimitIndex2() {
       
   540 
       
   541         $query = [
       
   542             'index' => env('ELASTICSEARCH_INDEX'),
       
   543             'body' => [
       
   544                 'size' => 0,
       
   545                 'aggs' => [
       
   546                     "subjects" => [
       
   547                         "nested" => [ "path" => "subject" ],
       
   548                         "aggs" => [
       
   549                             "subjects" => [
       
   550                                 "terms" => [
       
   551                                     "field" => "subject.label_code",
       
   552                                     "size" => 9,
       
   553                                     "order" => [ '_count' => 'desc' ],
       
   554                                     "include" => ".*\|bnf\|.*"
       
   555                                 ]
       
   556                             ]
       
   557                         ]
       
   558                     ]
       
   559                 ]
       
   560             ]
       
   561         ];
       
   562 
       
   563         // This is not normally what should be received, but we test that the limit is respected
       
   564         Es::shouldReceive('search')
       
   565             ->once()
       
   566             ->with($query)
       
   567             ->andReturn([
       
   568                 "took" => 27,
       
   569                 "timed_out" => false,
       
   570                 "_shards" => [
       
   571                     "total" => 1,
       
   572                     "successful" => 1,
       
   573                     "failed" => 0
       
   574                 ],
       
   575                 "hits" => [
       
   576                     "total" => 3011,
       
   577                     "max_score" => 0.0,
       
   578                     "hits" => [ ]
       
   579                 ],
       
   580                 "aggregations" => [
       
   581                     "subjects" => [
       
   582                         "doc_count" => 41524,
       
   583                         "subjects" => [
       
   584                             "doc_count_error_upper_bound" => 0,
       
   585                             "sum_other_doc_count" => 13678,
       
   586                             "buckets" => [ [
       
   587                                 "key" => "professions|bnf|ark:/12148/cb13318415c",
       
   588                                 "doc_count" => 1412
       
   589                             ], [
       
   590                                 "key" => "travail non rémunéré|bnf|ark:/12148/cb16604691s",
       
   591                                 "doc_count" => 1092
       
   592                             ], [
       
   593                                 "key" => "famille|bnf|ark:/12148/cb119339867",
       
   594                                 "doc_count" => 1050
       
   595                             ], [
       
   596                                 "key" => "oiseaux|bnf|ark:/12148/cb11932889r",
       
   597                                 "doc_count" => 1003
       
   598                             ], [
       
   599                                 "key" => "météorologie|bnf|ark:/12148/cb11932496x",
       
   600                                 "doc_count" => 1001
       
   601                             ], [
       
   602                                 "key" => "plantes|bnf|ark:/12148/cb11933145f",
       
   603                                 "doc_count" => 995
       
   604                             ], [
       
   605                                 "key" => "animaux sauvages|bnf|ark:/12148/cb11930908q",
       
   606                                 "doc_count" => 989
       
   607                             ], [
       
   608                                 "key" => "arbres|bnf|ark:/12148/cb11934786x",
       
   609                                 "doc_count" => 989
       
   610                             ] ]
       
   611                         ]
       
   612                     ]
       
   613                 ]
       
   614             ]);
       
   615         $this->get('/api/v1/stats/themes/?limit=3&index=2')->assertTrue($this->response->isOk(), $this->response->content());
       
   616         $this->seeJsonEquals(["themes" => [
       
   617             "http://ark.bnf.fr/ark:/12148/cb11930908q" => ['label' => 'animaux sauvages', 'count' => 989 ],
       
   618             "http://ark.bnf.fr/ark:/12148/cb11934786x" => ['label' => 'arbres', 'count' => 989 ]
       
   619         ]]);
       
   620     }
       
   621 
   156 }
   622 }