From 38df64522d4abf834609acb6f57928c0de4135fc Mon Sep 17 00:00:00 2001 From: mrey Date: Wed, 1 Feb 2017 13:06:42 +0100 Subject: [PATCH] TTK-13139 SchemaBundle: add findByTitleWithLocale Series repo function --- .../Repository/SeriesRepository.php | 57 +++++++++++++++++++ .../Tests/Repository/SeriesRepositoryTest.php | 48 ++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/src/Pumukit/SchemaBundle/Repository/SeriesRepository.php b/src/Pumukit/SchemaBundle/Repository/SeriesRepository.php index 41b155f02..f4ae121e1 100644 --- a/src/Pumukit/SchemaBundle/Repository/SeriesRepository.php +++ b/src/Pumukit/SchemaBundle/Repository/SeriesRepository.php @@ -564,6 +564,63 @@ public function findByEmbeddedBroadcastTypeAndGroups($type = '', $groups = array return $query->execute(); } + /** + * Find by title with locale query builder. + * + * @param string $search + * @param string $locale + * @param array $sort + * @param int $limit + * @param int $page + * + * @return QueryBuilder + */ + public function findByTitleWithLocaleQueryBuilder($title = '', $locale = 'en', $sort = array(), $limit = 0, $page = 0) + { + $qb = $this->createQueryBuilder() + ->field('title.'.$locale)->equals(new \MongoRegex(sprintf('/%s/i', $title))); + + $qb = $this->addSortAndLimitToQueryBuilder($qb, $sort, $limit, $page); + + return $qb; + } + + /** + * Find by title with locale query. + * + * @param string $search + * @param string $locale + * @param array $sort + * @param int $limit + * @param int $page + * + * @return Query + */ + public function findByTitleWithLocaleQuery($title = '', $locale = 'en', $sort = array(), $limit = 0, $page = 0) + { + $qb = $this->findByTitleWithLocaleQueryBuilder($title, $locale, $sort, $limit, $page); + + return $qb->getQuery(); + } + + /** + * Find by title with locale. + * + * @param string $search + * @param string $locale + * @param array $sort + * @param int $limit + * @param int $page + * + * @return Query + */ + public function findByTitleWithLocale($title = '', $locale = 'en', $sort = array(), $limit = 0, $page = 0) + { + $query = $this->findByTitleWithLocaleQuery($title, $locale, $sort, $limit, $page); + + return $query->execute(); + } + /** * Add limit (and page) to Query Builder. * diff --git a/src/Pumukit/SchemaBundle/Tests/Repository/SeriesRepositoryTest.php b/src/Pumukit/SchemaBundle/Tests/Repository/SeriesRepositoryTest.php index 916747f5c..1c9c70481 100644 --- a/src/Pumukit/SchemaBundle/Tests/Repository/SeriesRepositoryTest.php +++ b/src/Pumukit/SchemaBundle/Tests/Repository/SeriesRepositoryTest.php @@ -1273,6 +1273,54 @@ public function testFindByPersonIdAndRoleCodOrGroupsSorted() $this->assertTrue(in_array($series3, $seriesPerson2Role3Group2->toArray())); } + public function testfindByTitleWithLocale() + { + $test1 = 'test1'; + $test2 = 'test2'; + $prueba1 = 'prueba1'; + $prueba2 = 'prueba2'; + $enLocale = 'en'; + $esLocale = 'es'; + $series1I8nTitle = array($enLocale => $test1, $esLocale => $prueba1); + $series1 = new Series(); + $series1->setI18nTitle($series1I8nTitle); + + $series2I8nTitle = array($enLocale => $test2, $esLocale => $prueba2); + $series2 = new Series(); + $series2->setI18nTitle($series2I8nTitle); + + $this->dm->persist($series1); + $this->dm->persist($series2); + $this->dm->flush(); + + $seriesTest1En = $this->repo->findByTitleWithLocale($test1, $enLocale)->toArray(); + $seriesTest1Es = $this->repo->findByTitleWithLocale($test1, $esLocale)->toArray(); + $seriesPrueba1En = $this->repo->findByTitleWithLocale($prueba1, $enLocale)->toArray(); + $seriesPrueba1Es = $this->repo->findByTitleWithLocale($prueba1, $esLocale)->toArray(); + $seriesTest2En = $this->repo->findByTitleWithLocale($test2, $enLocale)->toArray(); + $seriesTest2Es = $this->repo->findByTitleWithLocale($test2, $esLocale)->toArray(); + $seriesPrueba2En = $this->repo->findByTitleWithLocale($prueba2, $enLocale)->toArray(); + $seriesPrueba2Es = $this->repo->findByTitleWithLocale($prueba2, $esLocale)->toArray(); + + $this->assertTrue(in_array($series1, $seriesTest1En)); + $this->assertFalse(in_array($series1, $seriesTest1Es)); + $this->assertFalse(in_array($series1, $seriesPrueba1En)); + $this->assertTrue(in_array($series1, $seriesPrueba1Es)); + $this->assertFalse(in_array($series1, $seriesTest2En)); + $this->assertFalse(in_array($series1, $seriesTest2Es)); + $this->assertFalse(in_array($series1, $seriesPrueba2En)); + $this->assertFalse(in_array($series1, $seriesPrueba2Es)); + + $this->assertFalse(in_array($series2, $seriesTest1En)); + $this->assertFalse(in_array($series2, $seriesTest1Es)); + $this->assertFalse(in_array($series2, $seriesPrueba1En)); + $this->assertFalse(in_array($series2, $seriesPrueba1Es)); + $this->assertTrue(in_array($series2, $seriesTest2En)); + $this->assertFalse(in_array($series2, $seriesTest2Es)); + $this->assertFalse(in_array($series2, $seriesPrueba2En)); + $this->assertTrue(in_array($series2, $seriesPrueba2Es)); + } + private function createSeriesType($name) { $description = 'description';