/** * Imports urls from file and returns number of items imported. * * @param string $file * @param int $redirectCode * @param bool $countRedirects * @param OutputInterface $output * * @throws Exception\CsvImporterException * @return int */ public function import($file, $redirectCode, $countRedirects, OutputInterface $output) { $count = 0; if (($handle = fopen($file, 'r')) !== false) { $em = $this->getEm(); while (($data = fgetcsv($handle)) !== false) { if (count($data) != 2) { throw new CsvImporterException(sprintf('There should be only 2 columns in csv file. Got %d columns.', count($data))); } $urlFrom = $data[0]; $urlTo = $data[1]; $map = new Map(); $map->setUrlFrom($urlFrom)->setUrlTo($urlTo)->setRedirectHttpCode($redirectCode)->setCountRedirects($countRedirects); if ($this->getValidator()->validate($map)) { $em->persist($map); $count++; } else { $output->writeln('<error>Circular redirect detected for ' . $map->getUrlTo() . '</error>'); } } fclose($handle); $em->flush(); } return $count; }
public function matchesPath() { $urlFrom = $this->map->getUrlFrom(); $requestUri = $this->request->getRequestUri(); if (!$this->map->getUrlFromIsRegexPattern()) { $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; return $this->map->getUrlFromIsNoCase() ? $strtolower($urlFrom) === $strtolower($requestUri) : $urlFrom === $requestUri; } $regexModifier = $this->map->getUrlFromIsNoCase() ? 'i' : ''; return preg_match('#' . $urlFrom . '#' . $regexModifier, $requestUri, $this->patternMatches); }
/** * Tests if method is working properly and if all mocks all called properly. */ public function testIfListenerSetProperRedirect() { $map = new Map(); $map->setUrlFrom('/something')->setUrlTo('/somewhere'); $repoMock = $this->getMockBuilder('Astina\\Bundle\\RedirectManagerBundle\\Entity\\MapRepository')->disableOriginalConstructor()->getMock(); $repoMock->expects($this->once())->method('findCandidatesForUrlOrPath')->will($this->returnValue(array($map))); $managerMock = $this->getMockBuilder('\\Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock(); $managerMock->expects($this->once())->method('persist')->with($map); $managerMock->expects($this->once())->method('getRepository')->will($this->returnValue($repoMock)); $redirectFinder = new RedirectFinder($managerMock); $redirectListener = new RedirectListener($redirectFinder); $requestMock = $this->getRequestMock('GET', null, '/something'); $eventMock = $this->getResponseEventMock($requestMock); $eventMock->expects($this->once())->method('setResponse'); $redirectListener->onKernelRequest($eventMock); $this->assertEquals(1, $map->getCount(), 'Map count should be increased to 1.'); }
protected function createRedirect($hostPattern, $urlFrom, $requestUrl, $isRegex = false, $isNoCase = false, $negate = false, $urlTo = null) { $request = Request::create($requestUrl); $map = new Map(); $map->setUrlFrom($urlFrom); $map->setUrlFromIsRegexPattern($isRegex); $map->setUrlFromIsNoCase($isNoCase); $map->setUrlTo($urlTo); $map->setHost($hostPattern); $map->setHostIsRegexPattern($isRegex); $map->setHostRegexPatternNegate($negate); return new Redirect($request, $map); }
/** * Check for circular redirects etc * @param Map $map * @return boolean */ public function validate(Map $map) { if ($map->getUrlFrom() == $map->getUrlTo()) { return false; } $self = $this; $accumulator = []; if ($map->getId()) { $accumulator = [$map->getId() => $map->getId()]; } $recurse = function (Map $map, Map $startPointMap) use($self, &$recurse, &$accumulator) { $matchingToUrls = $self->mapRepository->findForUrlOrPath($map->getUrlTo(), $map->getUrlTo(), $accumulator); foreach ($matchingToUrls as $match) { $accumulator[$match->getId()] = $match->getId(); if ($startPointMap->getUrlFrom() == $match->getUrlTo()) { return false; } elseif ($match->getUrlFrom() == $map->getUrlTo()) { return $recurse($match, $startPointMap); } } return true; }; return $recurse($map, $map); }
/** * @param int $httpCode * * @expectedException \Exception * @dataProvider httpCodeProvider */ public function testIfExceptionIsRaisedWhenWrongHttpCodeIsGiven($httpCode) { $map = new Map(); $map->setRedirectHttpCode($httpCode); }