From c2627beeeb88cb7c05be94728f10ef1babad0a7a Mon Sep 17 00:00:00 2001 From: Anne-Laure de Boissieu Date: Fri, 15 Oct 2021 16:15:36 +0200 Subject: [PATCH] delete brokenlink service --- README.md | 8 -- config/routing/accesseo.xml | 10 -- config/services.xml | 12 --- src/Checker/BrokenLinkChecker.php | 94 ---------------- src/Controller/BrokenLinkController.php | 51 --------- .../accessibility_collector.html.twig | 43 -------- .../BrokenLinkCheckerTest.php | 100 ------------------ tests/BrokenLinkChecker/broken-links.html | 16 --- 8 files changed, 334 deletions(-) delete mode 100644 config/routing/accesseo.xml delete mode 100644 src/Checker/BrokenLinkChecker.php delete mode 100644 src/Controller/BrokenLinkController.php delete mode 100644 tests/BrokenLinkChecker/BrokenLinkCheckerTest.php delete mode 100644 tests/BrokenLinkChecker/broken-links.html diff --git a/README.md b/README.md index ea86d5f..8533f3a 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,6 @@ in `config/bundles.php` add: Elao\Bundle\Accesseo\ElaoAccesseoBundle::class => ['dev' => true], ``` -in `config/routes/dev`, create `accesseo.yaml` and add: - -```yaml -accesseo: - resource: '@ElaoAccesseoBundle/config/routing/accesseo.xml' - prefix: /_accesseo -``` - ### Configuration options in `config/packages/elao_accesseo.yaml` : diff --git a/config/routing/accesseo.xml b/config/routing/accesseo.xml deleted file mode 100644 index 7fd5c87..0000000 --- a/config/routing/accesseo.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - Elao\Bundle\Accesseo\Controller\BrokenLinkController - - diff --git a/config/services.xml b/config/services.xml index 0e9c07e..5779cdb 100644 --- a/config/services.xml +++ b/config/services.xml @@ -18,17 +18,5 @@ - - - - - - - - - - - - diff --git a/src/Checker/BrokenLinkChecker.php b/src/Checker/BrokenLinkChecker.php deleted file mode 100644 index f35ebc9..0000000 --- a/src/Checker/BrokenLinkChecker.php +++ /dev/null @@ -1,94 +0,0 @@ -client = $client; - } - - public function getLinks(Crawler $crawler): ?array - { - return $crawler - ->filter('a') - ->extract(['href']); - } - - public function getExternalBrokenLinks(array $links): ?array - { - $urls = [ - 'errors' => [ - '403' => [], - '404' => [], - '500' => [], - '503' => [], - '504' => [], - 'invalid' => [], - 'timeout' => [], - ], - 'redirections' => [ - '301' => [], - '302' => [], - ], - 'success' => [ - '200' => [], - ], - ]; - - if (\count($links) === 0) { - return $urls; - } - - foreach ($links as $link) { - try { - $status = $this->getStatusCode($link); - } catch (TimeoutExceptionInterface $ex) { - $status = 'timeout'; - } catch (TransportExceptionInterface $ex) { - $status = 'invalid'; - } - - switch (true) { - case $status >= 300 && $status < 400: - $urls['redirections'][$status][] = $link; - break; - case $status === 200: - $urls['success'][$status][] = $link; - break; - default: - $urls['errors'][$status][] = $link; - } - } - - return $urls; - } - - /** - * @throws TransportExceptionInterface - */ - private function getStatusCode(string $uri): int - { - if (null === $this->client) { - throw new \LogicException(sprintf('You cannot use the "%s" class if no "symfony/http-client-contracts" implementation is available. Try running "composer require symfony/http-client".', __CLASS__)); - } - - $response = $this->client->request( - 'GET', - $uri - ); - - return $response->getStatusCode(); - } -} diff --git a/src/Controller/BrokenLinkController.php b/src/Controller/BrokenLinkController.php deleted file mode 100644 index df35831..0000000 --- a/src/Controller/BrokenLinkController.php +++ /dev/null @@ -1,51 +0,0 @@ -twig = $twig; - $this->profiler = $profiler; - $this->brokenLinkChecker = $brokenLinkChecker; - } - - public function __invoke(Request $request, $token): Response - { - $profile = $this->profiler->loadProfile($token); - - $linksFromDataCollector = $profile->getCollector('elao.accesseo.accessibility_collector')->getLinks(); - - $links = array_map(static function (Data $data): string {return $data->getValue(); }, $linksFromDataCollector); - - $links = $this->brokenLinkChecker->getExternalBrokenLinks($links); - - $template = $this->twig->render( - '@ElaoAccesseo/profiler/broken_links.html.twig', - [ - 'links' => $links, - 'countSuccess' => array_sum(array_map('count', $links['success'])), - 'countErrors' => array_sum(array_map('count', $links['errors'])), - 'countRedirections' => array_sum(array_map('count', $links['redirections'])), - ]); - - return new JsonResponse(['template' => $template]); - } -} diff --git a/templates/profiler/accessibility_collector.html.twig b/templates/profiler/accessibility_collector.html.twig index c204ad4..7be1a7a 100644 --- a/templates/profiler/accessibility_collector.html.twig +++ b/templates/profiler/accessibility_collector.html.twig @@ -160,49 +160,6 @@ padding: 0; } - {{ parent() }} {% endblock %} diff --git a/tests/BrokenLinkChecker/BrokenLinkCheckerTest.php b/tests/BrokenLinkChecker/BrokenLinkCheckerTest.php deleted file mode 100644 index ff956df..0000000 --- a/tests/BrokenLinkChecker/BrokenLinkCheckerTest.php +++ /dev/null @@ -1,100 +0,0 @@ -responsesFactory = new MockClientCallback()); - $this->checker = new BrokenLinkChecker($httpClient); - } - - /** - * @dataProvider provideGetLinksInformationsData - */ - public function testGetLinksInformations(string $filename, array $responses, array $expected): void - { - $html = file_get_contents(sprintf(__DIR__.'/../BrokenLinkChecker/%s', $filename)); - - $this->responsesFactory->addResponses(...array_values($responses)); - - static::assertEquals($expected, $this->checker->getExternalBrokenLinks($this->checker->getLinks(new Crawler($html)))); - } - - public function provideGetLinksInformationsData(): iterable - { - yield [ - 'filename' => 'broken-links.html', - 'responses' => [ - 'http://www.google.com' => ['info' => ['http_code' => 200]], - 'https://www.nasa.gov/' => ['info' => ['http_code' => 200]], - 'http://www.google.com/error' => ['info' => ['http_code' => 404]], - 'http://www.google/error' => new TransportException('invalid url'), - 'http://www.google/timeout' => new TimeoutException('timeout url'), - ], - 'expected' => [ - 'success' => [ - '200' => ['http://www.google.com', 'https://www.nasa.gov/'], - ], - 'redirections' => [ - '301' => [], - '302' => [], - ], - 'errors' => [ - '404' => ['http://www.google.com/error'], - '403' => [], - '500' => [], - '503' => [], - '504' => [], - 'invalid' => ['http://www.google/error'], - 'timeout' => ['http://www.google/timeout'], ], - ], - ]; - } -} - -class MockClientCallback -{ - /** @var array|ResponseInterface */ - private $responses = []; - - public function __invoke(string $method, string $url, array $options = []): ResponseInterface - { - if (false === $data = current($this->responses)) { - throw new \LogicException('No more mock response available'); - } - - next($this->responses); - - if ($data instanceof TransportException) { - throw $data; - } - - return $data instanceof ResponseInterface ? $data : new MockResponse($data['body'] ?? '', $data['info'] ?? []); - } - - public function addResponses(...$responses): void - { - foreach ($responses as $response) { - $this->responses[] = $response; - } - } -} diff --git a/tests/BrokenLinkChecker/broken-links.html b/tests/BrokenLinkChecker/broken-links.html deleted file mode 100644 index dc4aeda..0000000 --- a/tests/BrokenLinkChecker/broken-links.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - Title - - - - - - - - - - -