Skip to content

Commit

Permalink
Offline Mode: Fix incorrect predicates used when syncPublishing is off (
Browse files Browse the repository at this point in the history
  • Loading branch information
kean authored May 9, 2024
2 parents 746236e + 2178db3 commit df9aaee
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 159 deletions.
8 changes: 4 additions & 4 deletions WordPress/Classes/ViewRelated/Post/PostListFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import Foundation
let statuses: [BasePost.Status] = [.publish, .publishPrivate]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// existing published/private posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -128,7 +128,7 @@ import Foundation
let statusesForLocalDrafts: [BasePost.Status] = [.draft, .pending, .publish, .publishPrivate, .scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// Existing draft/pending posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -170,7 +170,7 @@ import Foundation
let statuses: [BasePost.Status] = [.scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// existing scheduled posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down Expand Up @@ -213,7 +213,7 @@ import Foundation
let statuses: [BasePost.Status] = [.draft, .pending, .publish, .publishPrivate, .scheduled]

let predicate: NSPredicate
if FeatureFlag.syncPublishing.enabled {
if !FeatureFlag.syncPublishing.enabled {
let query =
// Existing non-trashed posts
"(statusAfterSync = status AND status IN (%@))"
Expand Down
155 changes: 0 additions & 155 deletions WordPress/WordPressTest/PostListFilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,159 +50,4 @@ class PostListFilterTests: CoreDataTestCase {
XCTAssertEqual(descriptors[0].key, filter.sortField.keyPath)
}
}

func testDraftFilterIncludesLocalDraftsAndExistingDraftAndPendingPosts() {
// Arrange
let predicate = PostListFilter.draftFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.draft),
createPost(.publish),
createPost(.scheduled),
createPost(.publishPrivate),
createPost(.pending),
createPost(.draft, hasRemote: true),
createPost(.pending, hasRemote: true),
createPost(.draft, hasRemote: true, statusAfterSync: .draft),
createPost(.pending, hasRemote: true, statusAfterSync: .pending),
]
let nonMatchingPosts = [
createPost(.trash),
createPost(.deleted),

createPost(.publish, hasRemote: true),
createPost(.publishPrivate, hasRemote: true),
createPost(.scheduled, hasRemote: true),
createPost(.trash, hasRemote: true),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
expect(nonMatchingPosts).to(allPass { predicate.evaluate(with: $0) == false })
}

func testDraftFilterIncludesExistingDraftsAndPendingPostsTransitionedToOtherStatuses() {
// Arrange
let predicate = PostListFilter.draftFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.publish, hasRemote: true, statusAfterSync: .draft),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .draft),
createPost(.scheduled, hasRemote: true, statusAfterSync: .draft),
createPost(.pending, hasRemote: true, statusAfterSync: .draft),
createPost(.trash, hasRemote: true, statusAfterSync: .draft),

createPost(.publish, hasRemote: true, statusAfterSync: .pending),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .pending),
createPost(.scheduled, hasRemote: true, statusAfterSync: .pending),
createPost(.pending, hasRemote: true, statusAfterSync: .pending),
createPost(.trash, hasRemote: true, statusAfterSync: .pending),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
}

func testPublishedFilterIncludesExistingPrivateAndRemotePublishedPosts() {
// Arrange
let predicate = PostListFilter.publishedFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.publish, hasRemote: true),
createPost(.publishPrivate, hasRemote: true),
createPost(.publish, hasRemote: true, statusAfterSync: .publish),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .publishPrivate),
]
let nonMatchingPosts = [
createPost(.draft),
createPost(.publish),
createPost(.publishPrivate),
createPost(.scheduled),
createPost(.trash),
createPost(.deleted),

createPost(.draft, hasRemote: true),
createPost(.scheduled, hasRemote: true),
createPost(.trash, hasRemote: true),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
expect(nonMatchingPosts).to(allPass { predicate.evaluate(with: $0) == false })
}

func testPublishedFilterIncludesExistingPublishedAndPrivatePostsTransitionedToOtherStatuses() {
// Arrange
let predicate = PostListFilter.publishedFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.draft, hasRemote: true, statusAfterSync: .publish),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .publish),
createPost(.scheduled, hasRemote: true, statusAfterSync: .publish),
createPost(.pending, hasRemote: true, statusAfterSync: .publish),
createPost(.trash, hasRemote: true, statusAfterSync: .publish),

createPost(.draft, hasRemote: true, statusAfterSync: .publishPrivate),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .publishPrivate),
createPost(.scheduled, hasRemote: true, statusAfterSync: .publishPrivate),
createPost(.pending, hasRemote: true, statusAfterSync: .publishPrivate),
createPost(.trash, hasRemote: true, statusAfterSync: .publishPrivate),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
}

func testScheduledFilterIncludesExistingScheduledPostsOnly() {
// Arrange
let predicate = PostListFilter.scheduledFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.scheduled, hasRemote: true),
createPost(.scheduled, hasRemote: true, statusAfterSync: .scheduled),
]
let nonMatchingPosts = [
createPost(.draft),
createPost(.scheduled),
createPost(.publish),
createPost(.publishPrivate),
createPost(.trash),
createPost(.deleted),

createPost(.trash, hasRemote: true),
createPost(.draft, hasRemote: true),
createPost(.publish, hasRemote: true),
createPost(.publishPrivate, hasRemote: true),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
expect(nonMatchingPosts).to(allPass { predicate.evaluate(with: $0) == false })
}

func testScheduledFilterIncludesExistingScheduledPostsTransitionedToOtherStatuses() {
// Arrange
let predicate = PostListFilter.scheduledFilter().predicateForFetchRequest
let matchingPosts = [
createPost(.draft, hasRemote: true, statusAfterSync: .scheduled),
createPost(.publish, hasRemote: true, statusAfterSync: .scheduled),
createPost(.publishPrivate, hasRemote: true, statusAfterSync: .scheduled),
createPost(.pending, hasRemote: true, statusAfterSync: .scheduled),
createPost(.trash, hasRemote: true, statusAfterSync: .scheduled),
]

// Assert
expect(matchingPosts).to(allPass { predicate.evaluate(with: $0) == true })
}
}

private extension PostListFilterTests {
func createPost(_ status: BasePost.Status,
hasRemote: Bool = false,
statusAfterSync: BasePost.Status? = nil) -> Post {
let post = Post(context: mainContext)
post.status = status
post.statusAfterSync = statusAfterSync

if hasRemote {
post.postID = NSNumber(value: Int.random(in: 1...Int.max))
}

return post
}
}

0 comments on commit df9aaee

Please sign in to comment.