ImportServiceOutlinesTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. final class ImportServiceOutlinesTest extends PHPUnit\Framework\TestCase {
  4. #[\Override]
  5. protected function setUp(): void {
  6. if (!FreshRSS_Context::hasSystemConf()) {
  7. FreshRSS_Context::initSystem();
  8. }
  9. // Prevents any actual database connection
  10. FreshRSS_DatabaseDAO::$dummyConnection = true;
  11. }
  12. /**
  13. * @return array{0:array<array-key,array<string,string>>,1:array<array-key,list<array<string,string>>>}
  14. */
  15. private static function loadFromOutlines(string $opml): array {
  16. $libopml = new \marienfressinaud\LibOpml\LibOpml(strict: false);
  17. $opml_array = $libopml->parseString($opml);
  18. $importService = new FreshRSS_Import_Service();
  19. $method = new ReflectionMethod($importService, 'loadFromOutlines');
  20. /** @var array{0:array<array-key,array<string,string>>,1:array<array-key,list<array<string,string>>>} */
  21. return $method->invoke($importService, $opml_array['body'], '');
  22. }
  23. /**
  24. * numeric-string names for categories and feeds must survive OPML parsing and the merging of sibling outlines.
  25. */
  26. public function test_numeric_names_are_preserved(): void {
  27. [$categories_elements, $categories_to_feeds] = self::loadFromOutlines(<<<'XML'
  28. <?xml version="1.0" encoding="UTF-8"?>
  29. <opml version="2.0">
  30. <head><title>Test</title></head>
  31. <body>
  32. <outline text="90">
  33. <outline text="Feed 90" type="rss" xmlUrl="https://example.net/90.xml" />
  34. </outline>
  35. <outline text="42">
  36. <outline text="42 feed" type="rss" xmlUrl="https://example.net/42.xml" />
  37. </outline>
  38. <outline text="Github">
  39. <outline text="12345" type="rss" xmlUrl="https://example.net/gh.xml" />
  40. </outline>
  41. </body>
  42. </opml>
  43. XML);
  44. self::assertCount(3, $categories_elements);
  45. self::assertCount(3, $categories_to_feeds);
  46. self::assertSame([90, 42, 'Github'], array_keys($categories_elements), 'Numeric category keys are preserved while merging sibling outlines');
  47. self::assertSame('90', $categories_elements[90]['text']);
  48. self::assertSame('42', $categories_elements[42]['text']);
  49. foreach ($categories_to_feeds as $category_name => $feeds) {
  50. self::assertCount(1, $feeds, "Category `{$category_name}` keeps its feed");
  51. }
  52. self::assertSame('Feed 90', $categories_to_feeds[90][0]['text']);
  53. self::assertSame('42 feed', $categories_to_feeds[42][0]['text']);
  54. self::assertSame('12345', $categories_to_feeds['Github'][0]['text'], 'Numeric feed name is preserved');
  55. self::assertSame('https://example.net/90.xml', $categories_to_feeds[90][0]['xmlUrl']);
  56. self::assertSame('https://example.net/42.xml', $categories_to_feeds[42][0]['xmlUrl']);
  57. self::assertSame('https://example.net/gh.xml', $categories_to_feeds['Github'][0]['xmlUrl']);
  58. }
  59. }