A free, self-hostable news aggregator…
Fix feed ordering (#4841)
Alexis Degrugillier02b906549ede
diff --git a/app/Models/Category.php b/app/Models/Category.phpindex c4ca12fd3..b33bec26e 100644--- a/app/Models/Category.php+++ b/app/Models/Category.php@@ -103,9 +103,7 @@ class FreshRSS_Category extends Minz_Model {$this->hasFeedsWithError |= $feed->inError();}- usort($this->feeds, function ($a, $b) {- return strnatcasecmp($a->name(), $b->name());- });+ $this->sortFeeds();}return $this->feeds;@@ -144,6 +142,7 @@ class FreshRSS_Category extends Minz_Model {}$this->feeds = $values;+ $this->sortFeeds();}/**@@ -155,6 +154,8 @@ class FreshRSS_Category extends Minz_Model {$this->feeds = [];}$this->feeds[] = $feed;++ $this->sortFeeds();}public function _attributes($key, $value) {@@ -245,4 +246,10 @@ class FreshRSS_Category extends Minz_Model {return $ok;}++ private function sortFeeds() {+ usort($this->feeds, static function ($a, $b) {+ return strnatcasecmp($a->name(), $b->name());+ });+ }}diff --git a/tests/app/Models/CategoryTest.php b/tests/app/Models/CategoryTest.phpindex e4a921baa..93fbdbc63 100644--- a/tests/app/Models/CategoryTest.php+++ b/tests/app/Models/CategoryTest.php@@ -30,4 +30,54 @@ class CategoryTest extends PHPUnit\Framework\TestCase {);}+ public function test_feedOrdering() {+ $feed_1 = $this->getMockBuilder(FreshRSS_Feed::class)+ ->disableOriginalConstructor()+ ->getMock();+ $feed_1->expects($this->any())+ ->method('name')+ ->willReturn('AAA');++ $feed_2 = $this->getMockBuilder(FreshRSS_Feed::class)+ ->disableOriginalConstructor()+ ->getMock();+ $feed_2->expects($this->any())+ ->method('name')+ ->willReturn('ZZZ');++ $feed_3 = $this->getMockBuilder(FreshRSS_Feed::class)+ ->disableOriginalConstructor()+ ->getMock();+ $feed_3->expects($this->any())+ ->method('name')+ ->willReturn('lll');++ $category = new FreshRSS_Category('test', [+ $feed_1,+ $feed_2,+ $feed_3,+ ]);+ $feeds = $category->feeds();++ $this->assertCount(3, $feeds);+ $this->assertEquals('AAA', $feeds[0]->name());+ $this->assertEquals('lll', $feeds[1]->name());+ $this->assertEquals('ZZZ', $feeds[2]->name());++ $feed_4 = $this->getMockBuilder(FreshRSS_Feed::class)+ ->disableOriginalConstructor()+ ->getMock();+ $feed_4->expects($this->any())+ ->method('name')+ ->willReturn('BBB');++ $category->addFeed($feed_4);+ $feeds = $category->feeds();++ $this->assertCount(4, $feeds);+ $this->assertEquals('AAA', $feeds[0]->name());+ $this->assertEquals('BBB', $feeds[1]->name());+ $this->assertEquals('lll', $feeds[2]->name());+ $this->assertEquals('ZZZ', $feeds[3]->name());+ }}