From 1afdbc9ca1d823e13356944733c4c9af90ffe2e6 Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Wed, 27 Nov 2024 18:11:30 +0530 Subject: [PATCH 1/7] Use `wp/v2` endpoint for uploading media for all types of sites. --- Yosemite/Yosemite/Stores/MediaStore.swift | 55 ++++++----------------- 1 file changed, 14 insertions(+), 41 deletions(-) diff --git a/Yosemite/Yosemite/Stores/MediaStore.swift b/Yosemite/Yosemite/Stores/MediaStore.swift index 435a9af27c3..b0f6de145cd 100644 --- a/Yosemite/Yosemite/Stores/MediaStore.swift +++ b/Yosemite/Yosemite/Stores/MediaStore.swift @@ -151,51 +151,24 @@ private extension MediaStore { uploadableMedia media: UploadableMedia, shouldRemoveFileUponCompletion: Bool, onCompletion: @escaping (Result) -> Void) { - if isLoggedInWithoutWPCOMCredentials(siteID) || isSiteJetpackJCPConnected(siteID) { - remote.uploadMediaToWordPressSite(siteID: siteID, - productID: productID, - mediaItem: media) { result in - // Removes local media after the upload API request. - if shouldRemoveFileUponCompletion { - do { - try MediaFileManager().removeLocalMedia(at: media.localURL) - } catch { - onCompletion(.failure(error)) - return - } - } - - switch result { - case .success(let uploadedMedia): - onCompletion(.success(uploadedMedia.toMedia())) - case .failure(let error): + remote.uploadMediaToWordPressSite(siteID: siteID, + productID: productID, + mediaItem: media) { result in + // Removes local media after the upload API request. + if shouldRemoveFileUponCompletion { + do { + try MediaFileManager().removeLocalMedia(at: media.localURL) + } catch { onCompletion(.failure(error)) + return } } - } else { - remote.uploadMedia(for: siteID, - productID: productID, - mediaItems: [media]) { result in - // Removes local media after the upload API request. - if shouldRemoveFileUponCompletion { - do { - try MediaFileManager().removeLocalMedia(at: media.localURL) - } catch { - onCompletion(.failure(error)) - return - } - } - switch result { - case .success(let uploadedMediaItems): - guard let uploadedMedia = uploadedMediaItems.first, uploadedMediaItems.count == 1 else { - onCompletion(.failure(MediaActionError.unexpectedMediaCount(count: uploadedMediaItems.count))) - return - } - onCompletion(.success(uploadedMedia)) - case .failure(let error): - onCompletion(.failure(error)) - } + switch result { + case .success(let uploadedMedia): + onCompletion(.success(uploadedMedia.toMedia())) + case .failure(let error): + onCompletion(.failure(error)) } } } From 07c0bb55528332661cd87be12ce8be690e10a5e9 Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Wed, 27 Nov 2024 18:12:10 +0530 Subject: [PATCH 2/7] Remove unused media remote upload media method. --- .../Networking.xcodeproj/project.pbxproj | 4 -- .../Networking/Remote/MediaRemote.swift | 45 ---------------- .../Remote/MediaRemoteTests.swift | 53 ------------------- .../Responses/media-upload.json | 53 ------------------- 4 files changed, 155 deletions(-) delete mode 100644 Networking/NetworkingTests/Responses/media-upload.json diff --git a/Networking/Networking.xcodeproj/project.pbxproj b/Networking/Networking.xcodeproj/project.pbxproj index 901f7a6c24e..59acb3b5861 100644 --- a/Networking/Networking.xcodeproj/project.pbxproj +++ b/Networking/Networking.xcodeproj/project.pbxproj @@ -19,7 +19,6 @@ 020D07BC23D856BF00FD9580 /* MediaRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020D07BB23D856BF00FD9580 /* MediaRemote.swift */; }; 020D07BE23D8570800FD9580 /* MediaListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020D07BD23D8570800FD9580 /* MediaListMapper.swift */; }; 020D07C023D8587700FD9580 /* MediaRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020D07BF23D8587700FD9580 /* MediaRemoteTests.swift */; }; - 020D07C223D858BB00FD9580 /* media-upload.json in Resources */ = {isa = PBXBuildFile; fileRef = 020D07C123D858BB00FD9580 /* media-upload.json */; }; 020D0C03291504DE00BB3DCE /* UnauthenticatedRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020D0C02291504DE00BB3DCE /* UnauthenticatedRequestTests.swift */; }; 020EF5E92A8BC957009D2169 /* ProductsTotalMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020EF5E82A8BC957009D2169 /* ProductsTotalMapper.swift */; }; 020EF5ED2A8C8F4F009D2169 /* products-total.json in Resources */ = {isa = PBXBuildFile; fileRef = 020EF5EC2A8C8F4F009D2169 /* products-total.json */; }; @@ -1305,7 +1304,6 @@ 020D07BB23D856BF00FD9580 /* MediaRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaRemote.swift; sourceTree = ""; }; 020D07BD23D8570800FD9580 /* MediaListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaListMapper.swift; sourceTree = ""; }; 020D07BF23D8587700FD9580 /* MediaRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaRemoteTests.swift; sourceTree = ""; }; - 020D07C123D858BB00FD9580 /* media-upload.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "media-upload.json"; sourceTree = ""; }; 020D0C02291504DE00BB3DCE /* UnauthenticatedRequestTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnauthenticatedRequestTests.swift; sourceTree = ""; }; 020EF5E82A8BC957009D2169 /* ProductsTotalMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductsTotalMapper.swift; sourceTree = ""; }; 020EF5EC2A8C8F4F009D2169 /* products-total.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "products-total.json"; sourceTree = ""; }; @@ -3266,7 +3264,6 @@ 02F096C12406691100C0C1D5 /* media-library.json */, 02AF07EB27492FDD00B2D81E /* media-library-from-wordpress-site.json */, EECB7EE7286555180028C888 /* media-update-product-id.json */, - 020D07C123D858BB00FD9580 /* media-upload.json */, 02AF07ED27493AE700B2D81E /* media-upload-to-wordpress-site.json */, B58D10C92114D22E00107ED4 /* new-order-note.json */, 022902D322E2436400059692 /* no_stats_permission_error.json */, @@ -4692,7 +4689,6 @@ 0359EA2127AAE58C0048DE2D /* wcpay-charge-card-present.json in Resources */, 451274A625276C82009911FF /* product-variation.json in Resources */, CC80E3F92948C8BC00D5FF45 /* site-visits-quarter.json in Resources */, - 020D07C223D858BB00FD9580 /* media-upload.json in Resources */, CE71E23B2A4C666A00DB5376 /* reports-products-alt.json in Resources */, 31A451D127863A2E00FE81AA /* stripe-account-rejected-other.json in Resources */, DEF13C5029629EEA0024A02B /* user-complete-without-data.json in Resources */, diff --git a/Networking/Networking/Remote/MediaRemote.swift b/Networking/Networking/Remote/MediaRemote.swift index 7fb630ae3c2..31b76d60268 100644 --- a/Networking/Networking/Remote/MediaRemote.swift +++ b/Networking/Networking/Remote/MediaRemote.swift @@ -18,10 +18,6 @@ public protocol MediaRemoteProtocol { pageNumber: Int, pageSize: Int, completion: @escaping (Result<[WordPressMedia], Error>) -> Void) - func uploadMedia(for siteID: Int64, - productID: Int64, - mediaItems: [UploadableMedia], - completion: @escaping (Result<[Media], Error>) -> Void) func uploadMediaToWordPressSite(siteID: Int64, productID: Int64, mediaItem: UploadableMedia, @@ -145,47 +141,6 @@ public class MediaRemote: Remote, MediaRemoteProtocol { } } - /// Uploads an array of media in the local file system. - /// API reference: https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/media/new/ - /// - /// - Parameters: - /// - siteID: Site for which we'll upload the media to. - /// - productID: Product for which the media items are first added to. - /// - context: Display or edit. Scope under which the request is made; - /// determines fields present in response. Default is Display. - /// - mediaItems: An array of uploadable media items. - /// - completion: Closure to be executed upon completion. - /// - public func uploadMedia(for siteID: Int64, - productID: Int64, - mediaItems: [UploadableMedia], - completion: @escaping (Result<[Media], Error>) -> Void) { - - let formParameters: [String: String] = [Int](0.. Date: Wed, 27 Nov 2024 18:24:40 +0530 Subject: [PATCH 3/7] Rename media upload method. --- Networking/Networking.xcodeproj/project.pbxproj | 8 ++++---- Networking/Networking/Remote/MediaRemote.swift | 10 +++++----- .../NetworkingTests/Remote/MediaRemoteTests.swift | 14 +++++++------- ...ad-to-wordpress-site.json => media-upload.json} | 0 Yosemite/Yosemite/Stores/MediaStore.swift | 6 +++--- .../Mocks/Networking/Remote/MockMediaRemote.swift | 8 ++++---- 6 files changed, 23 insertions(+), 23 deletions(-) rename Networking/NetworkingTests/Responses/{media-upload-to-wordpress-site.json => media-upload.json} (100%) diff --git a/Networking/Networking.xcodeproj/project.pbxproj b/Networking/Networking.xcodeproj/project.pbxproj index 59acb3b5861..ef99e52580e 100644 --- a/Networking/Networking.xcodeproj/project.pbxproj +++ b/Networking/Networking.xcodeproj/project.pbxproj @@ -94,7 +94,6 @@ 02AED9DC2AA04716006DC460 /* KeyedDecodingContainer+WooTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02AED9DB2AA04716006DC460 /* KeyedDecodingContainer+WooTests.swift */; }; 02AF07EA27492DBC00B2D81E /* WordPressMedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02AF07E927492DBC00B2D81E /* WordPressMedia.swift */; }; 02AF07EC27492FDD00B2D81E /* media-library-from-wordpress-site.json in Resources */ = {isa = PBXBuildFile; fileRef = 02AF07EB27492FDD00B2D81E /* media-library-from-wordpress-site.json */; }; - 02AF07EE27493AE700B2D81E /* media-upload-to-wordpress-site.json in Resources */ = {isa = PBXBuildFile; fileRef = 02AF07ED27493AE700B2D81E /* media-upload-to-wordpress-site.json */; }; 02B41A90296BC85800FE3311 /* site-domains.json in Resources */ = {isa = PBXBuildFile; fileRef = 02B41A8F296BC85800FE3311 /* site-domains.json */; }; 02B41A92296BEB3000FE3311 /* load-site-current-plan-success.json in Resources */ = {isa = PBXBuildFile; fileRef = 02B41A91296BEB3000FE3311 /* load-site-current-plan-success.json */; }; 02B41A94296C04BC00FE3311 /* load-site-plans-no-current-plan.json in Resources */ = {isa = PBXBuildFile; fileRef = 02B41A93296C04BC00FE3311 /* load-site-plans-no-current-plan.json */; }; @@ -1247,6 +1246,7 @@ EECB7EE8286555180028C888 /* media-update-product-id.json in Resources */ = {isa = PBXBuildFile; fileRef = EECB7EE7286555180028C888 /* media-update-product-id.json */; }; EECDBEDE296845F400293C4E /* RESTRequestConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDBEDD296845F400293C4E /* RESTRequestConvertible.swift */; }; EECDBEE029684AC500293C4E /* WordPressAPIVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDBEDF29684AC500293C4E /* WordPressAPIVersionTests.swift */; }; + EED25B1D2CF74B9800503657 /* media-upload.json in Resources */ = {isa = PBXBuildFile; fileRef = EED25B1C2CF74B9800503657 /* media-upload.json */; }; EEDADD282B7C6A5E00F7302B /* media.json in Resources */ = {isa = PBXBuildFile; fileRef = EEDADD272B7C6A5E00F7302B /* media.json */; }; EEE846A22A54745F005239B6 /* identify-language-success.json in Resources */ = {isa = PBXBuildFile; fileRef = EEE846A02A54745F005239B6 /* identify-language-success.json */; }; EEE846A32A54745F005239B6 /* identify-language-failure.json in Resources */ = {isa = PBXBuildFile; fileRef = EEE846A12A54745F005239B6 /* identify-language-failure.json */; }; @@ -1379,7 +1379,6 @@ 02AED9DB2AA04716006DC460 /* KeyedDecodingContainer+WooTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KeyedDecodingContainer+WooTests.swift"; sourceTree = ""; }; 02AF07E927492DBC00B2D81E /* WordPressMedia.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressMedia.swift; sourceTree = ""; }; 02AF07EB27492FDD00B2D81E /* media-library-from-wordpress-site.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "media-library-from-wordpress-site.json"; sourceTree = ""; }; - 02AF07ED27493AE700B2D81E /* media-upload-to-wordpress-site.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "media-upload-to-wordpress-site.json"; sourceTree = ""; }; 02B41A8F296BC85800FE3311 /* site-domains.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "site-domains.json"; sourceTree = ""; }; 02B41A91296BEB3000FE3311 /* load-site-current-plan-success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "load-site-current-plan-success.json"; sourceTree = ""; }; 02B41A93296C04BC00FE3311 /* load-site-plans-no-current-plan.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "load-site-plans-no-current-plan.json"; sourceTree = ""; }; @@ -2433,6 +2432,7 @@ EECB7EE7286555180028C888 /* media-update-product-id.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "media-update-product-id.json"; sourceTree = ""; }; EECDBEDD296845F400293C4E /* RESTRequestConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RESTRequestConvertible.swift; sourceTree = ""; }; EECDBEDF29684AC500293C4E /* WordPressAPIVersionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressAPIVersionTests.swift; sourceTree = ""; }; + EED25B1C2CF74B9800503657 /* media-upload.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "media-upload.json"; sourceTree = ""; }; EEDADD272B7C6A5E00F7302B /* media.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = media.json; sourceTree = ""; }; EEE846A02A54745F005239B6 /* identify-language-success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "identify-language-success.json"; sourceTree = ""; }; EEE846A12A54745F005239B6 /* identify-language-failure.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "identify-language-failure.json"; sourceTree = ""; }; @@ -3082,6 +3082,7 @@ B559EBA820A0B5B100836CD4 /* Responses */ = { isa = PBXGroup; children = ( + EED25B1C2CF74B9800503657 /* media-upload.json */, EE6C6B6F2C6A190500632BDA /* systemStatus-inconsistent-page-id-data-type.json */, DEB3878E2C2D71A10025256E /* gla-campaign-list-with-data-envelope.json */, DEB3878D2C2D71A10025256E /* gla-campaign-list-without-data-envelope.json */, @@ -3264,7 +3265,6 @@ 02F096C12406691100C0C1D5 /* media-library.json */, 02AF07EB27492FDD00B2D81E /* media-library-from-wordpress-site.json */, EECB7EE7286555180028C888 /* media-update-product-id.json */, - 02AF07ED27493AE700B2D81E /* media-upload-to-wordpress-site.json */, B58D10C92114D22E00107ED4 /* new-order-note.json */, 022902D322E2436400059692 /* no_stats_permission_error.json */, B5A24178217F98F600595DEF /* notifications-load-all.json */, @@ -4279,7 +4279,6 @@ EE57C137297F98DB00BC31E7 /* product-variations-load-all-without-data.json in Resources */, 09885C8027C3FFD200910A62 /* product-variations-bulk-update.json in Resources */, 31054734262E36AB00C5C02B /* wcpay-payment-intent-error.json in Resources */, - 02AF07EE27493AE700B2D81E /* media-upload-to-wordpress-site.json in Resources */, CCE5F38F29EFFE3800087332 /* subscription-list.json in Resources */, DE4F2A4029750EF400B0701C /* inbox-note-without-data.json in Resources */, 45ED4F12239E8C57004F1BE3 /* taxes-classes.json in Resources */, @@ -4616,6 +4615,7 @@ 02E7FFCF25621C7900C53030 /* shipping-label-print.json in Resources */, CE12AE9529F29F4F0056DD17 /* order-with-subscription-renewal.json in Resources */, CEE9188129F7DC23004B23FF /* order-with-gift-cards.json in Resources */, + EED25B1D2CF74B9800503657 /* media-upload.json in Resources */, 74ABA1CB213F19FE00FFAD30 /* top-performers-week.json in Resources */, B9CA4F332AB213DF00285AB9 /* tax.json in Resources */, 456930AD2652AF00009ED69D /* shipping-label-carriers-and-rates-success.json in Resources */, diff --git a/Networking/Networking/Remote/MediaRemote.swift b/Networking/Networking/Remote/MediaRemote.swift index 31b76d60268..d0e26f1dd27 100644 --- a/Networking/Networking/Remote/MediaRemote.swift +++ b/Networking/Networking/Remote/MediaRemote.swift @@ -18,7 +18,7 @@ public protocol MediaRemoteProtocol { pageNumber: Int, pageSize: Int, completion: @escaping (Result<[WordPressMedia], Error>) -> Void) - func uploadMediaToWordPressSite(siteID: Int64, + func uploadMedia(siteID: Int64, productID: Int64, mediaItem: UploadableMedia, completion: @escaping (Result) -> Void) @@ -150,10 +150,10 @@ public class MediaRemote: Remote, MediaRemoteProtocol { /// - productID: Product for which the media items are first added to. /// - mediaItem: The media item to upload. /// - completion: Closure to be executed upon completion. - public func uploadMediaToWordPressSite(siteID: Int64, - productID: Int64, - mediaItem: UploadableMedia, - completion: @escaping (Result) -> Void) { + public func uploadMedia(siteID: Int64, + productID: Int64, + mediaItem: UploadableMedia, + completion: @escaping (Result) -> Void) { let formParameters: [String: String] = [ ParameterKey.wordPressMediaPostID: "\(productID)", ParameterKey.fieldsWordPressSite: ParameterValue.wordPressMediaFields, diff --git a/Networking/NetworkingTests/Remote/MediaRemoteTests.swift b/Networking/NetworkingTests/Remote/MediaRemoteTests.swift index 3b97cef4908..b25877423ab 100644 --- a/Networking/NetworkingTests/Remote/MediaRemoteTests.swift +++ b/Networking/NetworkingTests/Remote/MediaRemoteTests.swift @@ -212,7 +212,7 @@ final class MediaRemoteTests: XCTestCase { let remote = MediaRemote(network: network) // When - remote.uploadMediaToWordPressSite(siteID: sampleSiteID, productID: sampleProductID, mediaItem: .fake(), completion: { _ in }) + remote.uploadMedia(siteID: sampleSiteID, productID: sampleProductID, mediaItem: .fake(), completion: { _ in }) // Then let request = try XCTUnwrap(network.requestsForResponseData.last as? DotcomRequest) @@ -229,9 +229,9 @@ final class MediaRemoteTests: XCTestCase { // When let result = waitFor { promise in - remote.uploadMediaToWordPressSite(siteID: self.sampleSiteID, - productID: self.sampleProductID, - mediaItem: .fake()) { result in + remote.uploadMedia(siteID: self.sampleSiteID, + productID: self.sampleProductID, + mediaItem: .fake()) { result in promise(result) } } @@ -263,9 +263,9 @@ final class MediaRemoteTests: XCTestCase { // When let result = waitFor { promise in - remote.uploadMediaToWordPressSite(siteID: self.sampleSiteID, - productID: self.sampleProductID, - mediaItem: .fake()) { result in + remote.uploadMedia(siteID: self.sampleSiteID, + productID: self.sampleProductID, + mediaItem: .fake()) { result in promise(result) } } diff --git a/Networking/NetworkingTests/Responses/media-upload-to-wordpress-site.json b/Networking/NetworkingTests/Responses/media-upload.json similarity index 100% rename from Networking/NetworkingTests/Responses/media-upload-to-wordpress-site.json rename to Networking/NetworkingTests/Responses/media-upload.json diff --git a/Yosemite/Yosemite/Stores/MediaStore.swift b/Yosemite/Yosemite/Stores/MediaStore.swift index b0f6de145cd..14e84cacc14 100644 --- a/Yosemite/Yosemite/Stores/MediaStore.swift +++ b/Yosemite/Yosemite/Stores/MediaStore.swift @@ -151,9 +151,9 @@ private extension MediaStore { uploadableMedia media: UploadableMedia, shouldRemoveFileUponCompletion: Bool, onCompletion: @escaping (Result) -> Void) { - remote.uploadMediaToWordPressSite(siteID: siteID, - productID: productID, - mediaItem: media) { result in + remote.uploadMedia(siteID: siteID, + productID: productID, + mediaItem: media) { result in // Removes local media after the upload API request. if shouldRemoveFileUponCompletion { do { diff --git a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift index 8a3f6c85752..749ede73062 100644 --- a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift +++ b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift @@ -131,10 +131,10 @@ extension MockMediaRemote: MediaRemoteProtocol { completion(result) } - func uploadMediaToWordPressSite(siteID: Int64, - productID: Int64, - mediaItem: UploadableMedia, - completion: @escaping (Result) -> Void) { + func uploadMedia(siteID: Int64, + productID: Int64, + mediaItem: UploadableMedia, + completion: @escaping (Result) -> Void) { invocations.append(.uploadMediaToWordPressSite(siteID: siteID)) guard let result = uploadMediaToWordPressSiteResultsBySiteID[siteID] else { XCTFail("\(String(describing: self)) Could not find result for site ID: \(siteID)") From 96c46c9e49ac9e2f84f214906dccb12d7c73c70e Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Wed, 27 Nov 2024 21:39:03 +0530 Subject: [PATCH 4/7] Fix failing unit tests by updating mocks. --- .../Remote/MediaRemoteTests.swift | 8 ++--- .../Networking/Remote/MockMediaRemote.swift | 29 +++---------------- .../Stores/MediaStoreTests.swift | 20 ++++++------- 3 files changed, 18 insertions(+), 39 deletions(-) diff --git a/Networking/NetworkingTests/Remote/MediaRemoteTests.swift b/Networking/NetworkingTests/Remote/MediaRemoteTests.swift index b25877423ab..9b0f1916285 100644 --- a/Networking/NetworkingTests/Remote/MediaRemoteTests.swift +++ b/Networking/NetworkingTests/Remote/MediaRemoteTests.swift @@ -207,7 +207,7 @@ final class MediaRemoteTests: XCTestCase { // MARK: - uploadMedia - func test_uploadMediaToWordPressSite_does_not_send_data_in_request_body() throws { + func test_uploadMedia_does_not_send_data_in_request_body() throws { // Given let remote = MediaRemote(network: network) @@ -219,13 +219,13 @@ final class MediaRemoteTests: XCTestCase { XCTAssertNil(try request.asURLRequest().httpBody) } - /// Verifies that `uploadMediaToWordPressSite` properly parses the `media-upload-to-wordpress-site` sample response. + /// Verifies that `uploadMedia` properly parses the `media-upload` sample response. /// - func test_uploadMediaToWordPressSite_properly_returns_parsed_media() throws { + func test_uploadMedia_properly_returns_parsed_media() throws { // Given let remote = MediaRemote(network: network) let path = "sites/\(sampleSiteID)/media" - network.simulateResponse(requestUrlSuffix: path, filename: "media-upload-to-wordpress-site") + network.simulateResponse(requestUrlSuffix: path, filename: "media-upload") // When let result = waitFor { promise in diff --git a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift index 749ede73062..722d9f9c3bd 100644 --- a/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift +++ b/Yosemite/YosemiteTests/Mocks/Networking/Remote/MockMediaRemote.swift @@ -19,10 +19,7 @@ final class MockMediaRemote { private var loadMediaLibraryFromWordPressSiteResultsBySiteID = [Int64: Result<[WordPressMedia], Error>]() /// The results to return based on the given site ID in `uploadMedia` - private var uploadMediaResultsBySiteID = [Int64: Result<[Media], Error>]() - - /// The results to return based on the given site ID in `uploadMediaToWordPressSite` - private var uploadMediaToWordPressSiteResultsBySiteID = [Int64: Result]() + private var uploadMediaResultsBySiteID = [Int64: Result]() /// The results to return based on the given site ID in `updateProductID` private var updateProductIDResultsBySiteID = [Int64: Result]() @@ -46,15 +43,10 @@ final class MockMediaRemote { } /// Returns the value as a publisher when `uploadMedia` is called. - func whenUploadingMedia(siteID: Int64, thenReturn result: Result<[Media], Error>) { + func whenUploadingMedia(siteID: Int64, thenReturn result: Result) { uploadMediaResultsBySiteID[siteID] = result } - /// Returns the value as a publisher when `uploadMediaToWordPressSite` is called. - func whenUploadingMediaToWordPressSite(siteID: Int64, thenReturn result: Result) { - uploadMediaToWordPressSiteResultsBySiteID[siteID] = result - } - /// Returns the value as a publisher when `updateProductID` is called. func whenUpdatingProductID(siteID: Int64, thenReturn result: Result) { updateProductIDResultsBySiteID[siteID] = result @@ -72,7 +64,6 @@ extension MockMediaRemote { case loadMediaLibrary(siteID: Int64) case loadMediaLibraryFromWordPressSite(siteID: Int64) case uploadMedia(siteID: Int64) - case uploadMediaToWordPressSite(siteID: Int64) case updateProductID(siteID: Int64) case updateProductIDToWordPressSite(siteID: Int64) } @@ -119,24 +110,12 @@ extension MockMediaRemote: MediaRemoteProtocol { completion(result) } - func uploadMedia(for siteID: Int64, - productID: Int64, - mediaItems: [UploadableMedia], - completion: @escaping (Result<[Media], Error>) -> Void) { - invocations.append(.uploadMedia(siteID: siteID)) - guard let result = uploadMediaResultsBySiteID[siteID] else { - XCTFail("\(String(describing: self)) Could not find result for site ID: \(siteID)") - return - } - completion(result) - } - func uploadMedia(siteID: Int64, productID: Int64, mediaItem: UploadableMedia, completion: @escaping (Result) -> Void) { - invocations.append(.uploadMediaToWordPressSite(siteID: siteID)) - guard let result = uploadMediaToWordPressSiteResultsBySiteID[siteID] else { + invocations.append(.uploadMedia(siteID: siteID)) + guard let result = uploadMediaResultsBySiteID[siteID] else { XCTFail("\(String(describing: self)) Could not find result for site ID: \(siteID)") return } diff --git a/Yosemite/YosemiteTests/Stores/MediaStoreTests.swift b/Yosemite/YosemiteTests/Stores/MediaStoreTests.swift index f99820fd9c7..a2b29873464 100644 --- a/Yosemite/YosemiteTests/Stores/MediaStoreTests.swift +++ b/Yosemite/YosemiteTests/Stores/MediaStoreTests.swift @@ -350,7 +350,7 @@ final class MediaStoreTests: XCTestCase { let mediaStore = createMediaStoreAndExportableMedia(at: targetURL, fileManager: fileManager) - let path = "sites/\(sampleSiteID)/media/new" + let path = "sites/\(sampleSiteID)/media" network.simulateResponse(requestUrlSuffix: path, filename: "media-upload") let asset = PHAsset() @@ -386,7 +386,7 @@ final class MediaStoreTests: XCTestCase { let mediaStore = createMediaStoreAndExportableMedia(at: targetURL, fileManager: fileManager) - let path = "sites/\(sampleSiteID)/media/new" + let path = "sites/\(sampleSiteID)/media" network.simulateResponse(requestUrlSuffix: path, filename: "generic_error") @@ -459,7 +459,7 @@ final class MediaStoreTests: XCTestCase { let remote = MockMediaRemote() let media = WordPressMedia.fake() - remote.whenUploadingMediaToWordPressSite(siteID: siteID, thenReturn: .success(media)) + remote.whenUploadingMedia(siteID: siteID, thenReturn: .success(media)) let mediaStore = createMediaStoreAndExportableMedia(at: targetURL, fileManager: fileManager, remote: remote) @@ -478,7 +478,7 @@ final class MediaStoreTests: XCTestCase { } // Then - XCTAssertEqual(remote.invocations, [.uploadMediaToWordPressSite(siteID: siteID)]) + XCTAssertEqual(remote.invocations, [.uploadMedia(siteID: siteID)]) let mediaList = try XCTUnwrap(result.get()) XCTAssertEqual(mediaList, media.toMedia()) @@ -501,7 +501,7 @@ final class MediaStoreTests: XCTestCase { let remote = MockMediaRemote() let media = WordPressMedia.fake() - remote.whenUploadingMediaToWordPressSite(siteID: sampleSiteID, thenReturn: .success(media)) + remote.whenUploadingMedia(siteID: sampleSiteID, thenReturn: .success(media)) let mediaStore = createMediaStoreAndExportableMedia(at: targetURL, fileManager: fileManager, remote: remote) @@ -522,7 +522,7 @@ final class MediaStoreTests: XCTestCase { } // Then - XCTAssertEqual(remote.invocations, [.uploadMediaToWordPressSite(siteID: sampleSiteID)]) + XCTAssertEqual(remote.invocations, [.uploadMedia(siteID: sampleSiteID)]) let mediaList = try XCTUnwrap(result.get()) XCTAssertEqual(mediaList, media.toMedia()) @@ -543,7 +543,7 @@ final class MediaStoreTests: XCTestCase { }() let remote = MockMediaRemote() - remote.whenUploadingMediaToWordPressSite(siteID: sampleSiteID, thenReturn: .failure(DotcomError.unauthorized)) + remote.whenUploadingMedia(siteID: sampleSiteID, thenReturn: .failure(DotcomError.unauthorized)) let mediaStore = createMediaStoreAndExportableMedia(at: targetURL, fileManager: fileManager, remote: remote) @@ -564,7 +564,7 @@ final class MediaStoreTests: XCTestCase { } // Then - XCTAssertEqual(remote.invocations, [.uploadMediaToWordPressSite(siteID: sampleSiteID)]) + XCTAssertEqual(remote.invocations, [.uploadMedia(siteID: sampleSiteID)]) let error = try XCTUnwrap(result.failure as? DotcomError) XCTAssertEqual(error, .unauthorized) @@ -582,7 +582,7 @@ final class MediaStoreTests: XCTestCase { return fileManager.temporaryDirectory.appendingPathComponent(filename, isDirectory: false) }() - let path = "sites/\(sampleSiteID)/media/new" + let path = "sites/\(sampleSiteID)/media" network.simulateResponse(requestUrlSuffix: path, filename: "media-upload") let mediaStore = createMediaStoreAndExportableMedia(at: fileURL, fileManager: fileManager) @@ -613,7 +613,7 @@ final class MediaStoreTests: XCTestCase { return fileManager.temporaryDirectory.appendingPathComponent(filename, isDirectory: false) }() - let path = "sites/\(sampleSiteID)/media/new" + let path = "sites/\(sampleSiteID)/media" network.simulateResponse(requestUrlSuffix: path, filename: "generic_error") let mediaStore = createMediaStoreAndExportableMedia(at: fileURL, fileManager: fileManager) From 168cc5ac43245c29cda45e023f1552b4ca9939c4 Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Wed, 27 Nov 2024 21:40:09 +0530 Subject: [PATCH 5/7] Update release notes. --- RELEASE-NOTES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 7f8933bcb0d..9e65750faa8 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -5,6 +5,7 @@ - [*] Jetpack Setup: Fixed an issue where the WordPress.com authentication fails when using a passwordless account that's already connected to Jetpack [https://github.com/woocommerce/woocommerce-ios/pull/14501] - [*] Jetpack Setup: Fixed an issue with magic link handling when the app is cold started. [https://github.com/woocommerce/woocommerce-ios/pull/14502] - [**] Media Library: On sites logged in with application password, when picking image from WordPress Media Library, all images will now load correctly. [https://github.com/woocommerce/woocommerce-ios/pull/14444] +- [*] Media: Media upload will work for sites where XML-RPC is disabled. [https://github.com/woocommerce/woocommerce-ios/pull/14537] 21.2 ----- From ee3227b6d80dccd07ceee773e0245799f86b031e Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Fri, 29 Nov 2024 11:23:21 +0530 Subject: [PATCH 6/7] Update RELEASE-NOTES.txt Co-authored-by: Huong Do --- RELEASE-NOTES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 9e65750faa8..bc9ad629e62 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -5,7 +5,7 @@ - [*] Jetpack Setup: Fixed an issue where the WordPress.com authentication fails when using a passwordless account that's already connected to Jetpack [https://github.com/woocommerce/woocommerce-ios/pull/14501] - [*] Jetpack Setup: Fixed an issue with magic link handling when the app is cold started. [https://github.com/woocommerce/woocommerce-ios/pull/14502] - [**] Media Library: On sites logged in with application password, when picking image from WordPress Media Library, all images will now load correctly. [https://github.com/woocommerce/woocommerce-ios/pull/14444] -- [*] Media: Media upload will work for sites where XML-RPC is disabled. [https://github.com/woocommerce/woocommerce-ios/pull/14537] +- [*] Media: Media upload will work for sites without XML-RPC. [https://github.com/woocommerce/woocommerce-ios/pull/14537] 21.2 ----- From de07dbdb5e199fa0fe51a922c277dc4bf935956f Mon Sep 17 00:00:00 2001 From: Sharma Elanthiraiyan Date: Fri, 29 Nov 2024 11:26:04 +0530 Subject: [PATCH 7/7] Fix indentation. --- Networking/Networking/Remote/MediaRemote.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Networking/Networking/Remote/MediaRemote.swift b/Networking/Networking/Remote/MediaRemote.swift index d0e26f1dd27..f32d5593c98 100644 --- a/Networking/Networking/Remote/MediaRemote.swift +++ b/Networking/Networking/Remote/MediaRemote.swift @@ -19,9 +19,9 @@ public protocol MediaRemoteProtocol { pageSize: Int, completion: @escaping (Result<[WordPressMedia], Error>) -> Void) func uploadMedia(siteID: Int64, - productID: Int64, - mediaItem: UploadableMedia, - completion: @escaping (Result) -> Void) + productID: Int64, + mediaItem: UploadableMedia, + completion: @escaping (Result) -> Void) func updateProductID(siteID: Int64, productID: Int64, mediaID: Int64,