Skip to content

Commit

Permalink
fixed 410 expty target in import (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored and trickreich committed Oct 4, 2018
1 parent 00efc73 commit e702686
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Import/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Symfony\Component\HttpFoundation\Response;

/**
* Write redirect-route entity to database by using the entity-manager.
Expand Down Expand Up @@ -112,7 +113,7 @@ private function save(RedirectRouteInterface $entity)
*/
private function validate(RedirectRouteInterface $entity)
{
if ('' === $entity->getTarget()) {
if ('' === $entity->getTarget() && Response::HTTP_GONE !== $entity->getStatusCode()) {
throw new TargetIsEmptyException($entity);
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/Unit/Import/Writer/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\RedirectBundle\Import\Writer\DuplicatedSourceException;
use Sulu\Bundle\RedirectBundle\Import\Writer\TargetIsEmptyException;
use Sulu\Bundle\RedirectBundle\Import\Writer\Writer;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteManagerInterface;
use Sulu\Bundle\RedirectBundle\Manager\RedirectRouteNotUniqueException;
use Sulu\Bundle\RedirectBundle\Model\RedirectRouteInterface;
use Symfony\Component\HttpFoundation\Response;

class WriterTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -154,6 +156,34 @@ public function testWriteAlreadyExisting()
$this->writer->write($entity->reveal());
}

public function testWriteEmptyTarget()
{
$this->setExpectedException(TargetIsEmptyException::class);

$entity = $this->prophesize(RedirectRouteInterface::class);
$entity->getSource()->willReturn('/source');
$entity->getTarget()->willReturn('');
$entity->getStatusCode()->willReturn(Response::HTTP_MOVED_PERMANENTLY);

$this->redirectRouteManager->save($entity->reveal())->shouldNotBeCalled();

$this->writer->write($entity->reveal());
}

public function testWriteEmptyTargetFor410()
{
$entity = $this->prophesize(RedirectRouteInterface::class);
$entity->getSource()->willReturn('/source');
$entity->getTarget()->willReturn('');
$entity->getStatusCode()->willReturn(Response::HTTP_GONE);

$this->redirectRouteManager->save($entity->reveal())->shouldBeCalled();

$this->writer->write($entity->reveal());

$this->redirectRouteManager->save($entity->reveal())->shouldBeCalled();
}

public function testFinalize()
{
$this->writer->finalize();
Expand Down

0 comments on commit e702686

Please sign in to comment.