Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
27.6
-----
* [*] Stats: Fix a crash when expanding two years with the same month and views in a post's Months and Years or Avg. Views Per Day details [#26415]
* [*] Stats: Fix a crash when expanding two Tags and Categories groups that share a tag, two Clicks groups with the same link, or two Authors with the same post [#26462]
* [*] Application Passwords: Revoke passwords for other apps from the password details screen.
* [*] Fix signing in to WordPress.com taking longer than it needs to, and waiting on the login screen while the device is busy [#26121]
* [*] Notifications: Comment and like notifications from private sites you can't access now show their content instead of an error [#26432]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import Foundation
import Testing
import UIKit
import WordPressKit
import WordPressSharedObjC
@testable import WordPress

/// Covers detail tables where two expanded parents hold an equal child: a tag in two tag
/// groups, a link in two Clicks groups, and a post under two authors.
///
/// The suite is serialized because expanded rows live in the static
/// `StatsDataHelper.expandedRowLabelsDetails`.
@MainActor
@Suite(.serialized)
struct SiteStatsDetailsChildRowsTests {

@Test func tagInTwoExpandedGroupsShowsUnderEach() throws {
let table = DetailsTable(section: .insightsTagsAndCategories)
let tag = { (name: String) in ["name": name, "type": "tag", "link": "https://example.com/tag/\(name)/"] }
table.insightsStore.state.allTagsAndCategories = StatsTagsAndCategoriesInsight(jsonDictionary: [
"tags": [
["views": 20, "tags": [tag("news"), tag("tech")]],
["views": 10, "tags": [tag("news"), tag("sport")]]
] as AnyObject
])
table.insightsStore.state.allTagsAndCategoriesStatus = .success

table.expand(["news, tech", "news, sport"])

#expect(childNames(in: try table.snapshot()) == ["news", "tech", "news", "sport"])
}

@Test func linkInTwoExpandedClicksGroupsShowsUnderEach() throws {
let table = DetailsTable(section: .periodClicks)
let link = { (path: String, views: Int) in
["name": "example.com/\(path)/", "url": "https://example.com/\(path)/", "views": views] as [String: Any]
}
table.periodStore.state.topClicks = StatsTopClicksTimeIntervalData(
date: table.date,
period: .week,
jsonDictionary: table.periodResponse([
"clicks": [
["name": "Group one", "views": 20, "children": [link("shared", 4), link("one", 16)]],
["name": "Group two", "views": 10, "children": [link("shared", 4), link("two", 6)]]
],
"total_clicks": 30,
"other_clicks": 0
])
)
table.periodStore.state.topClicksStatus = .success

table.expand(["Group one", "Group two"])

#expect(
childNames(in: try table.snapshot()) == [
"example.com/shared/", "example.com/one/", "example.com/shared/", "example.com/two/"
]
)
}

@Test func coAuthoredPostShowsUnderBothExpandedAuthors() throws {
let table = DetailsTable(section: .periodAuthors)
let post = { (id: Int, title: String, views: Int) in
["id": id, "title": title, "url": "https://example.com/?p=\(id)", "views": views] as [String: Any]
}
let sharedPost = post(1, "Co-authored post", 5)
table.periodStore.state.topAuthors = StatsTopAuthorsTimeIntervalData(
date: table.date,
period: .week,
jsonDictionary: table.periodResponse([
"authors": [
["name": "Author 1", "avatar": "", "views": 9, "posts": [sharedPost, post(2, "Post A", 4)]],
["name": "Author 2", "avatar": "", "views": 8, "posts": [sharedPost, post(3, "Post B", 3)]]
],
"other_views": 0
])
)
table.periodStore.state.topAuthorsStatus = .success

table.expand(["Author 1", "Author 2"])

#expect(childNames(in: try table.snapshot()) == ["Co-authored post", "Post A", "Co-authored post", "Post B"])
}
}

// MARK: - Helpers

private extension SiteStatsDetailsChildRowsTests {
func childNames(in snapshot: ImmuTableDiffableDataSourceSnapshot) -> [String] {
snapshot.itemIdentifiers.compactMap { ($0.immuTableRow as? DetailExpandableChildRow)?.rowData.name }
}
}

/// A stats detail table backed by real stores.
///
/// Each test creates its own instance and releases it on the main actor: the stores'
/// `Dispatcher` asserts when their receipts are released on another thread.
@MainActor
private final class DetailsTable {
let date = Date()
let insightsStore = StatsInsightsStore()
let periodStore = StatsPeriodStore()
private let section: StatSection
private let viewModel: SiteStatsDetailsViewModel
// Strong references: the view model keeps its delegates weak.
private let detailsHandler = MockDetailsDelegate()
private let referrerHandler = MockReferrerDelegate()
private let savedExpandedRows = StatsDataHelper.expandedRowLabelsDetails

/// Set the store state after `init`: `fetchDataFor` can mark the section as loading.
init(section: StatSection) {
self.section = section
viewModel = SiteStatsDetailsViewModel(
detailsDelegate: detailsHandler,
referrerDelegate: referrerHandler,
insightsStore: insightsStore,
periodStore: periodStore
)
viewModel.fetchDataFor(statSection: section, selectedDate: date, selectedPeriod: .week)
}

deinit {
StatsDataHelper.expandedRowLabelsDetails = savedExpandedRows
}

func expand(_ names: [String]) {
StatsDataHelper.expandedRowLabelsDetails[section] = names
}

/// Wraps one day of a period endpoint's response. The parsers read only `days`.
func periodResponse(_ day: [String: Any]) -> [String: AnyObject] {
["days": ["2026-11-23": day]] as [String: AnyObject]
}

/// UIKit raises `NSInternalInconsistencyException` for equal snapshot items. Catching it
/// fails the test instead of ending the test process.
func snapshot() throws -> ImmuTableDiffableDataSourceSnapshot {
var snapshot = ImmuTableDiffableDataSourceSnapshot()
try WPException.objcTry {
snapshot = self.viewModel.tableViewSnapshot()
}
return snapshot
}

private final class MockDetailsDelegate: SiteStatsDetailsDelegate {}

private final class MockReferrerDelegate: SiteStatsReferrerDelegate {
func showReferrerDetails(_ data: StatsTotalRowData) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ private extension SiteStatsDetailsViewModel {
// If this child has no children, add it as a child row.
guard !grandChildRowsData.isEmpty else {
detailDataRows.append(childRow(rowData: childRowData,
parentNames: [rowData.name],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: As the description says, parents are told apart by name only, so two parents with the same name and an equal child still crash. I confirmed this with a local test: two authors both named "Alex" sharing a post, both expanded, makes tableViewSnapshot() throw NSInternalInconsistencyException. It throws on trunk too, so this PR doesn't make it worse. Optional follow-up: use the parent's position instead of its name (for example parentPath: [idx] / [idx, childIdx]). It's unique even when names repeat, and a count change still leaves unchanged children equal. A reorder would only reload rows.

hideFullSeparator: hideFullSeparator,
showImage: showImage))
continue
Expand Down Expand Up @@ -988,6 +989,7 @@ private extension SiteStatsDetailsViewModel {
let hideFullSeparator = (idx == grandChildRowsData.endIndex - 1) ? nextChildExpanded : true

detailDataRows.append(childRow(rowData: grandChildRowData,
parentNames: [rowData.name, childRowData.name],
hideFullSeparator: hideFullSeparator,
showImage: showImage))
}
Expand All @@ -997,8 +999,9 @@ private extension SiteStatsDetailsViewModel {
return detailDataRows
}

func childRow(rowData: StatsTotalRowData, hideFullSeparator: Bool, showImage: Bool) -> DetailExpandableChildRow {
func childRow(rowData: StatsTotalRowData, parentNames: [String], hideFullSeparator: Bool, showImage: Bool) -> DetailExpandableChildRow {
return DetailExpandableChildRow(rowData: rowData,
parentNames: parentNames,
detailsDelegate: detailsDelegate,
hideIndentedSeparator: true,
hideFullSeparator: hideFullSeparator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ private extension SiteStatsInsightsDetailsViewModel {
// If this child has no children, add it as a child row.
guard !grandChildRowsData.isEmpty else {
detailDataRows.append(childRow(rowData: childRowData,
parentNames: [rowData.name],
hideFullSeparator: hideFullSeparator,
showImage: showImage))
continue
Expand Down Expand Up @@ -1249,6 +1250,7 @@ private extension SiteStatsInsightsDetailsViewModel {
let hideFullSeparator = (idx == grandChildRowsData.endIndex - 1) ? nextChildExpanded : true

detailDataRows.append(childRow(rowData: grandChildRowData,
parentNames: [rowData.name, childRowData.name],
hideFullSeparator: hideFullSeparator,
showImage: showImage))
}
Expand All @@ -1258,8 +1260,9 @@ private extension SiteStatsInsightsDetailsViewModel {
return detailDataRows
}

func childRow(rowData: StatsTotalRowData, hideFullSeparator: Bool, showImage: Bool) -> DetailExpandableChildRow {
func childRow(rowData: StatsTotalRowData, parentNames: [String], hideFullSeparator: Bool, showImage: Bool) -> DetailExpandableChildRow {
return DetailExpandableChildRow(rowData: rowData,
parentNames: parentNames,
detailsDelegate: detailsDelegate,
hideIndentedSeparator: true,
hideFullSeparator: hideFullSeparator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ struct DetailExpandableChildRow: HashableImmutableRow {
}()

let rowData: StatsTotalRowData
/// Names of the rows this row is nested under, outermost first. Two expanded parents can
/// hold equal children, such as a tag in two tag groups, and the diffable table rejects
/// equal rows. Names rather than whole rows, so a parent's new count does not replace
/// its unchanged children.
let parentNames: [String]
weak var detailsDelegate: SiteStatsDetailsDelegate?
let hideIndentedSeparator: Bool
let hideFullSeparator: Bool
Expand All @@ -840,6 +845,7 @@ struct DetailExpandableChildRow: HashableImmutableRow {

static func == (lhs: DetailExpandableChildRow, rhs: DetailExpandableChildRow) -> Bool {
return lhs.rowData == rhs.rowData &&
lhs.parentNames == rhs.parentNames &&
lhs.hideIndentedSeparator == rhs.hideIndentedSeparator &&
lhs.hideFullSeparator == rhs.hideFullSeparator &&
lhs.showImage == rhs.showImage
Expand Down