ExtensionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}} License
  19. /**
  20. * This file test the Macro that Embeds SVG icons.
  21. *
  22. * @package Tests
  23. *
  24. * @author Ângelo D. Moura <up201303828@fe.up.pt>
  25. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  26. * @author Hugo Sales <hugo@hsal.es>
  27. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  28. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  29. */
  30. namespace App\Tests\Twig;
  31. use App\Twig\Extension;
  32. use App\Twig\Runtime;
  33. use App\Util\GNUsocialTestCase;
  34. use DirectoryIterator;
  35. use Symfony\Component\HttpFoundation\Request;
  36. class ExtensionTest extends GNUsocialTestCase
  37. {
  38. public function testIconsExtension()
  39. {
  40. // Get all Icon files names from "public/assets/icons"
  41. $icon_file_names = [];
  42. foreach (new DirectoryIterator('public/assets/icons/') as $file) {
  43. if ($file->isDot()) {
  44. continue;
  45. }
  46. $icon_file_names[] = $file->getFilename();
  47. }
  48. // Check if every icon file has either ".svg.twig" extension or ".svg"
  49. $twig_icon_file_names = [];
  50. foreach ($icon_file_names as $icon_file_name) {
  51. static::assertMatchesRegularExpression('/\.svg\.twig$|\.svg$/', $icon_file_name);
  52. if (preg_match('/\.svg\.twig$/', $icon_file_name, $matches, \PREG_OFFSET_CAPTURE, 0)) {
  53. unset($matches);
  54. $twig_icon_file_names[] = $icon_file_name;
  55. }
  56. }
  57. unset($icon_file_names);
  58. //Check if the function gives a valid HTML with a class attribute equal to the one passed
  59. static::bootKernel();
  60. $container = self::$kernel->getContainer()->get('test.service_container');
  61. $twig = $container->get('twig');
  62. foreach ($twig_icon_file_names as $icon_file_name) {
  63. $icon_name = basename($icon_file_name, '.svg.twig');
  64. $icon_template_render = $twig->render('@public_path/assets/icons/' . $icon_file_name, ['iconClass' => 'icon icon-' . $icon_name]);
  65. $icons_extension = new Runtime();
  66. $icon_extension_render = $icons_extension->embedSvgIcon($twig, $icon_name, 'icon icon-' . $icon_name);
  67. static::assertSame($icon_template_render, $icon_extension_render);
  68. }
  69. }
  70. public function testIsCurrentRouteActive()
  71. {
  72. $req = $this->createMock(Request::class);
  73. // @phpstan-ignore-next-line
  74. $req->attributes = new class {
  75. public function get(string $arg)
  76. {
  77. return 'current_route';
  78. }
  79. };
  80. $runtime = new Runtime;
  81. $runtime->setRequest($req);
  82. static::assertSame('active', $runtime->isCurrentRouteActive('current_route'));
  83. static::assertSame('', $runtime->isCurrentRouteActive('some_route', 'some_other_route'));
  84. }
  85. }