ExtensionTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Core\DB\DB;
  32. use App\Twig\Extension;
  33. use App\Twig\Runtime;
  34. use App\Util\GNUsocialTestCase;
  35. use DirectoryIterator;
  36. use Symfony\Component\HttpFoundation\Request;
  37. class ExtensionTest extends GNUsocialTestCase
  38. {
  39. public function testIconsExtension()
  40. {
  41. // Get all Icon files names from "public/assets/icons"
  42. $icon_file_names = [];
  43. foreach (new DirectoryIterator('public/assets/icons/') as $file) {
  44. if ($file->isDot()) {
  45. continue;
  46. }
  47. $icon_file_names[] = $file->getFilename();
  48. }
  49. // Check if every icon file has either ".svg.twig" extension or ".svg"
  50. $twig_icon_file_names = [];
  51. foreach ($icon_file_names as $icon_file_name) {
  52. static::assertMatchesRegularExpression('/\.svg\.twig$|\.svg$/', $icon_file_name);
  53. if (preg_match('/\.svg\.twig$/', $icon_file_name, $matches, \PREG_OFFSET_CAPTURE, 0)) {
  54. unset($matches);
  55. $twig_icon_file_names[] = $icon_file_name;
  56. }
  57. }
  58. unset($icon_file_names);
  59. //Check if the function gives a valid HTML with a class attribute equal to the one passed
  60. static::bootKernel();
  61. $container = self::$kernel->getContainer()->get('test.service_container');
  62. $twig = $container->get('twig');
  63. foreach ($twig_icon_file_names as $icon_file_name) {
  64. $icon_name = basename($icon_file_name, '.svg.twig');
  65. $icon_template_render = $twig->render('@public_path/assets/icons/' . $icon_file_name, ['iconClass' => 'icon icon-' . $icon_name]);
  66. $icons_extension = new Runtime();
  67. $icon_extension_render = $icons_extension->embedSvgIcon($twig, $icon_name, 'icon icon-' . $icon_name);
  68. static::assertSame($icon_template_render, $icon_extension_render);
  69. }
  70. }
  71. public function testIsCurrentRouteActive()
  72. {
  73. $req = $this->createMock(Request::class);
  74. // @phpstan-ignore-next-line
  75. $req->attributes = new class {
  76. public function get(string $arg)
  77. {
  78. return 'current_route';
  79. }
  80. };
  81. $runtime = new Runtime;
  82. $runtime->setRequest($req);
  83. static::assertSame('active', $runtime->isCurrentRouteActive('current_route'));
  84. static::assertSame('', $runtime->isCurrentRouteActive('some_route', 'some_other_route'));
  85. }
  86. public function testGetNoteActions()
  87. {
  88. static::bootKernel();
  89. $req = $this->createMock(Request::class);
  90. $runtime = new Runtime;
  91. $runtime->setRequest($req);
  92. static::assertSame([], $runtime->getNoteActions(DB::dql('select n from note n where n.content = \'some content\'')[0]));
  93. }
  94. }