/**
* @dataProvider entityManagerDataProvider
* @param EntityManager $em
*/
public function testRebuildIndex(EntityManager $em)
{
// Create an article with a slug
$article = new SluggedArticle();
$article->setId(8343)->setName('foo')->setSlug('bar');
$em->persist($article)->flush();
// Confirm slug works
/** @var SluggedArticle $article */
$article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
$this->assertEquals('foo', $article->getName());
$index = $em->getMapper()->getEntityMetadata($article)->getIndexByName('slug');
// Corrupt the slug, two steps required:
// 1. Set a new slug
$em->getDriver()->setSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'evil'), $article->getId());
// 2. Remove the correct slug
$em->getDriver()->clearSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'bar'));
$em->getDriver()->flush();
// Confirm old slug no longer works
try {
$em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
$this->fail('Old index succeeded');
} catch (NotFoundException $e) {
}
// Confirm new slug does work
$article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
$this->assertEquals('foo', $article->getName());
// Run maintenance over the table, this should correct the slug
$maintenance = new Maintenance($em);
$maintenance->rebuild(SluggedArticle::class);
// Confirm correct slug works
$article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false);
$this->assertEquals('foo', $article->getName());
// The corrupted slug should still work, this is unideal, but there is no reference to it for the maintenance
// service to know to remove it
$article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false);
$this->assertEquals('foo', $article->getName());
}