From 6325c9ed1c0a097c5a020223d7f66eca6d2dd35d Mon Sep 17 00:00:00 2001 From: Habibie Date: Thu, 19 Nov 2026 13:08:55 +0700 Subject: [PATCH 1/2] Fix: getFeeds() error jika item RSS tidak punya author dan gagal per feed (#1799) - DesaService::getFeeds() memakai nullsafe get_author()?->get_name() dengan fallback 'Administrator', sehingga item tanpa author tidak lagi dibuang. - Kegagalan satu feed desa dicatat (Log::warning) lalu dilewati, sehingga feed desa lain tetap tampil dan hasil kosong tidak ikut di-cache. - Ekstraksi makeFeedReader() dan mapFeedItem() agar dapat diuji tanpa jaringan. - Helper getFeeds() mendelegasikan ke DesaService; hapus import FeedsFacade (paket willvincent/feeds tidak terpasang). - Tambah test regresi DesaServiceFeedsTest. Co-Authored-By: Claude Opus 5.5 --- app/Services/DesaService.php | 77 ++++++---- helpers/general_helper.php | 31 +--- tests/Unit/Services/DesaServiceFeedsTest.php | 142 +++++++++++++++++++ tests/Unit/Services/DesaServiceTest.php | 1 - 4 files changed, 190 insertions(+), 61 deletions(-) create mode 100644 tests/Unit/Services/DesaServiceFeedsTest.php diff --git a/app/Services/DesaService.php b/app/Services/DesaService.php index f1182445e..2c70a3481 100644 --- a/app/Services/DesaService.php +++ b/app/Services/DesaService.php @@ -32,7 +32,6 @@ namespace App\Services; use App\Models\DataDesa; -use Exception; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; @@ -73,7 +72,7 @@ public function listDesa($all = false) } /** - * Get Unique Desa + * Get Unique Desa. */ public function desa(array $filters = []) { @@ -97,7 +96,7 @@ public function desa(array $filters = []) } /** - * Get feeds for all desa + * Get feeds for all desa. */ public function getFeeds(): array { @@ -109,14 +108,17 @@ public function getFeeds(): array })->filter(static fn($q) => !empty($q->website)); $feeds = []; foreach ($allDesa as $desa) { - $feedReader = new SimplePie(); - $feedReader->set_feed_url($desa->website_url_feed['website']); - //$feedReader->set_item_limit(5); - $feedReader->force_fsockopen(true); - $feedReader->set_cache_location(storage_path('framework/cache/simplepie')); - $feedReader->init(); - $feedReader->handle_content_type(); - $items = $feedReader->get_items(); + // Kegagalan satu feed desa tidak boleh menggagalkan feed desa lain + try { + $items = $this->makeFeedReader($desa->website_url_feed['website'])->get_items(); + } catch (\Throwable $feedError) { + Log::warning('Gagal mengambil feed desa', [ + 'desa_id' => $desa->desa_id, + 'website' => $desa->website, + 'message' => $feedError->getMessage(), + ]); + continue; + } // Handle case where no items returned if (empty($items)) { @@ -129,19 +131,7 @@ public function getFeeds(): array foreach ($items as $item) { try { - $feeds[] = [ - 'desa_id' => $desa->desa_id, - 'nama_desa' => $desa->nama, - 'feed_link' => $item->get_feed()->get_permalink(), - 'feed_title' => $item->get_feed()->get_title(), - 'link' => $item->get_link(), - 'date' => \Carbon\Carbon::createFromTimestamp((int) $item->get_date('U')), - 'author' => $item->get_author()->get_name() ?? 'Administrator', - 'title' => $item->get_title(), - 'image' => get_tag_image($item->get_description()), - 'description' => strip_tags(substr(str_replace(['&', 'nbsp;', '[...]'], '', $item->get_description()), 0, 250) . '[...]'), - 'content' => $item->get_content(), - ]; + $feeds[] = $this->mapFeedItem($item, $desa); } catch (\Throwable $itemError) { Log::error('Error processing feed item', [ 'desa_id' => $desa->desa_id, @@ -154,7 +144,7 @@ public function getFeeds(): array Log::info('Total feeds collected', ['count' => count($feeds)]); return $feeds; - } catch (Exception $e) { + } catch (\Throwable $e) { Log::error('Error fetching desa feeds', ['message' => $e->getMessage()]); return []; } @@ -171,7 +161,7 @@ public function listPathDesa() } /** - * Get Unique Desa + * Get Unique Desa. */ public function jumlahDesa(array $filters = []) { @@ -197,14 +187,41 @@ public function jumlahDesa(array $filters = []) } /** - * Dapatkan object desa berdasarkan kode_desa tertentu - * - * @param string $kodeDesa - * @return collection|DataDesa|\stdClass|null + * Dapatkan object desa berdasarkan kode_desa tertentu. */ public function getDesaByKode(string $kodeDesa): Collection|DataDesa|\stdClass|null { $listDesa = $this->listDesa(); return $listDesa->where('desa_id', $kodeDesa)->first(); } + + protected function makeFeedReader(string $url): SimplePie + { + $feedReader = new SimplePie(); + $feedReader->set_feed_url($url); + $feedReader->force_fsockopen(true); + $feedReader->set_cache_location(storage_path('framework/cache/simplepie')); + $feedReader->init(); + $feedReader->handle_content_type(); + + return $feedReader; + } + + protected function mapFeedItem($item, DataDesa $desa): array + { + return [ + 'desa_id' => $desa->desa_id, + 'nama_desa' => $desa->nama, + 'feed_link' => $item->get_feed()->get_permalink(), + 'feed_title' => $item->get_feed()->get_title(), + 'link' => $item->get_link(), + 'date' => \Carbon\Carbon::createFromTimestamp((int) $item->get_date('U')), + // get_author() mengembalikan null bila item feed tidak memiliki author + 'author' => $item->get_author()?->get_name() ?? 'Administrator', + 'title' => $item->get_title(), + 'image' => get_tag_image($item->get_description()), + 'description' => strip_tags(substr(str_replace(['&', 'nbsp;', '[...]'], '', $item->get_description()), 0, 250) . '[...]'), + 'content' => $item->get_content(), + ]; + } } diff --git a/helpers/general_helper.php b/helpers/general_helper.php index badfabeb9..2d7fa2056 100644 --- a/helpers/general_helper.php +++ b/helpers/general_helper.php @@ -30,7 +30,6 @@ */ use App\Http\Controllers\Helpers\Parsedown; -use App\Models\DataDesa; use App\Models\Menu; use App\Models\Role; use App\Models\Themes; @@ -40,7 +39,6 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Storage; use voku\helper\AntiXSS; -use willvincent\Feeds\Facades\FeedsFacade; /* * Parsing url image dari rss feed description @@ -550,34 +548,7 @@ function theme_active() if (! function_exists('getFeeds')) { function getFeeds() { - return cache()->remember('feeds_desa', 60 * 60, function () { - $all_desa = DataDesa::websiteUrl()->get() - ->map(function ($desa) { - return $desa->website_url_feed; - })->all(); - - $feeds = []; - foreach ($all_desa as $desa) { - $getFeeds = FeedsFacade::make($desa['website'], 5, true); - foreach ($getFeeds->get_items() as $item) { - $feeds[] = [ - 'desa_id' => $desa['desa_id'], - 'nama_desa' => $desa['nama'], - 'feed_link' => $item->get_feed()->get_permalink(), - 'feed_title' => $item->get_feed()->get_title(), - 'link' => $item->get_link(), - 'date' => \Carbon\Carbon::parse($item->get_date('U')), - 'author' => $item->get_author()->get_name() ?? 'Administrator', - 'title' => $item->get_title(), - 'image' => get_tag_image($item->get_description()), - 'description' => strip_tags(substr(str_replace(['&', 'nbsp;', '[...]'], '', $item->get_description()), 0, 250).'[...]'), - 'content' => $item->get_content(), - ]; - } - } - - return $feeds ?? null; - }); + return app(\App\Services\DesaService::class)->getFeeds(); } } diff --git a/tests/Unit/Services/DesaServiceFeedsTest.php b/tests/Unit/Services/DesaServiceFeedsTest.php new file mode 100644 index 000000000..0bee15253 --- /dev/null +++ b/tests/Unit/Services/DesaServiceFeedsTest.php @@ -0,0 +1,142 @@ +' + .'' + .'Feed Desahttps://desa.test/Berita' + .$itemXml + .''; +} + +/** + * DesaService tanpa akses jaringan: daftar desa dan isi feed ditentukan oleh test. + * + * @param array $feeds website desa => XML RSS atau exception + */ +function desaServiceDenganFeed(array $feeds): DesaService +{ + return new class($feeds) extends DesaService + { + public function __construct(private array $feeds) + { + parent::__construct(); + } + + public function listDesa($all = false) + { + return collect(array_keys($this->feeds))->values()->map(fn ($website, $i) => new DataDesa([ + 'desa_id' => 'DESA'.$i, + 'nama' => 'Desa '.$i, + 'sebutan_desa' => 'Desa', + 'website' => $website, + ])); + } + + protected function makeFeedReader(string $url): SimplePie + { + $website = str_replace('index.php/feed', '', $url); + $feed = $this->feeds[$website]; + + if ($feed instanceof Throwable) { + throw $feed; + } + + $reader = new SimplePie(); + $reader->enable_cache(false); + $reader->set_raw_data($feed); + $reader->init(); + + return $reader; + } + }; +} + +beforeEach(function () { + Cache::forget('desa_feeds'); +}); + +afterEach(function () { + Cache::forget('desa_feeds'); +}); + +it('memakai author Administrator bila item feed tidak punya author', function () { + $feeds = desaServiceDenganFeed([ + 'https://desa-a.test/' => rssFeed('Berita Tanpa Authorhttps://desa-a.test/artikel/1Isi'), + ])->getFeeds(); + + expect($feeds)->toHaveCount(1) + ->and($feeds[0]['title'])->toBe('Berita Tanpa Author') + ->and($feeds[0]['author'])->toBe('Administrator'); +}); + +it('tetap memakai nama author dari feed bila tersedia', function () { + $feeds = desaServiceDenganFeed([ + 'https://desa-a.test/' => rssFeed('Beritahttps://desa-a.test/artikel/1IsiOperator Desa'), + ])->getFeeds(); + + expect($feeds)->toHaveCount(1) + ->and($feeds[0]['author'])->toBe('Operator Desa'); +}); + +it('melewati feed desa yang gagal dan tetap mengambil feed desa lain', function () { + Log::spy(); + + $feeds = desaServiceDenganFeed([ + 'https://desa-rusak.test/' => new RuntimeException('Koneksi gagal'), + 'https://desa-b.test/' => rssFeed('Berita Desa Bhttps://desa-b.test/artikel/1Isi'), + ])->getFeeds(); + + expect($feeds)->toHaveCount(1) + ->and($feeds[0]['title'])->toBe('Berita Desa B') + ->and($feeds[0]['desa_id'])->toBe('DESA1'); + + Log::shouldHaveReceived('warning') + ->withArgs(fn ($message, $context) => $message === 'Gagal mengambil feed desa' && $context['desa_id'] === 'DESA0') + ->once(); +}); + +it('helper getFeeds mendelegasikan ke DesaService', function () { + $hasil = [['title' => 'Berita', 'author' => 'Administrator']]; + + $this->mock(DesaService::class) + ->shouldReceive('getFeeds') + ->once() + ->andReturn($hasil); + + expect(getFeeds())->toBe($hasil); +}); diff --git a/tests/Unit/Services/DesaServiceTest.php b/tests/Unit/Services/DesaServiceTest.php index 487d122d0..21b48f1b1 100644 --- a/tests/Unit/Services/DesaServiceTest.php +++ b/tests/Unit/Services/DesaServiceTest.php @@ -7,7 +7,6 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Config; -use willvincent\Feeds\Facades\FeedsFacade; uses(DatabaseTransactions::class); From 77f0d8b8f3c5ec3cf5d74d7fa87246746bbc63c1 Mon Sep 17 00:00:00 2001 From: Abah Roland Date: Thu, 26 Nov 2026 03:35:33 +0700 Subject: [PATCH 2/2] update catatan rilis --- catatan_rilis.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/catatan_rilis.md b/catatan_rilis.md index 5ab68a903..258ba7b19 100644 --- a/catatan_rilis.md +++ b/catatan_rilis.md @@ -22,6 +22,8 @@ Terimakasih [isi disini] yang telah berkontribusi langsung mengembangkan aplikas 11. [#1796](https://github.com/OpenSID/OpenDK/issues/1796) Menghapus Jabatan Camat/Sekretaris membuat seluruh aplikasi error 500. 12. [#1797](https://github.com/OpenSID/OpenDK/issues/1797) Persentase belanja barang/jasa salah dan division by zero di AnggaranRealisasiChartService. 13. [#1798](https://github.com/OpenSID/OpenDK/issues/1798) Perbaikan Cache listDesa terus diperpanjang dan tidak membedakan parameter $all. +14. [#1799](https://github.com/OpenSID/OpenDK/issues/1799) Perbaikan getFeeds() error jika item RSS tidak punya author dan gagal per feed. + #### TEKNIS