diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 58b296d603d4..86dd814098b1 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -307,17 +307,17 @@ PublisherInfoDatabase::GetPanelPublisher( } sql::Statement info_sql(db_.GetUniqueStatement( - "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, " - "pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi " - "LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id " - "WHERE pi.publisher_id=? AND ((ai.month = ? " - "AND ai.year = ? AND ai.reconcile_stamp = ?) OR " - "ai.percent IS NULL) LIMIT 1")); + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " + "pi.provider, pi.verified, pi.excluded, " + "(" + "SELECT IFNULL(percent, 0) FROM activity_info WHERE " + "publisher_id = ? AND reconcile_stamp = ? " + ") as percent " + "FROM publisher_info AS pi WHERE pi.publisher_id = ? LIMIT 1")); info_sql.BindString(0, filter.id); - info_sql.BindInt(1, filter.month); - info_sql.BindInt(2, filter.year); - info_sql.BindInt64(3, filter.reconcile_stamp); + info_sql.BindInt64(1, filter.reconcile_stamp); + info_sql.BindString(2, filter.id); if (info_sql.Step()) { std::unique_ptr info; diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 14966c230bdb..8cafbf6f7b89 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -405,4 +405,65 @@ TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) { } +TEST_F(PublisherInfoDatabaseTest, GetPanelPublisher) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + /** + * Publisher ID is missing + */ + ledger::ActivityInfoFilter filter_1; + EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr); + + /** + * Empty table + */ + ledger::ActivityInfoFilter filter_2; + filter_2.id = "test"; + EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr); + + /** + * Ignore month and year filter + */ + ledger::PublisherInfo info_1; + info_1.id = "brave.com"; + info_1.url = "https://brave.com"; + info_1.percent = 11; + info_1.month = ledger::ACTIVITY_MONTH::JANUARY; + info_1.year = 2019; + info_1.reconcile_stamp = 10; + + bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1); + EXPECT_TRUE(success); + + ledger::ActivityInfoFilter filter_3; + filter_3.id = "brave.com"; + filter_3.month = ledger::ACTIVITY_MONTH::ANY; + filter_3.year = -1; + filter_3.reconcile_stamp = 10; + std::unique_ptr result = + publisher_info_database_->GetPanelPublisher(filter_3); + EXPECT_TRUE(result); + EXPECT_EQ(result->id, "brave.com"); + + /** + * Still get data if reconcile stamp is not found + */ + info_1.id = "page.com"; + info_1.url = "https://page.com"; + info_1.reconcile_stamp = 9; + + success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1); + EXPECT_TRUE(success); + + ledger::ActivityInfoFilter filter_4; + filter_4.id = "page.com"; + filter_4.reconcile_stamp = 10; + result = publisher_info_database_->GetPanelPublisher(filter_4); + EXPECT_TRUE(result); + EXPECT_EQ(result->id, "page.com"); + EXPECT_EQ(result->percent, 0u); +} + } // namespace brave_rewards