Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTK-13139 SchemaBundle: add findByTitleWithLocale Series repo function #292

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Pumukit/SchemaBundle/Repository/SeriesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Pumukit/SchemaBundle/Tests/Repository/SeriesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down