-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMediaServiceUpdateTests.m
305 lines (273 loc) · 12.2 KB
/
MediaServiceUpdateTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#import <XCTest/XCTest.h>
#import "Blog.h"
#import "WordPressTest-Swift.h"
@import WordPressKit;
@import OCMock;
// Re-implement `MediaService` to mock the remote service `MediaServiceRemote`.
@interface MediaServiceForStubbing : MediaService
@property (nonatomic, strong) MediaServiceRemoteREST *remoteForStubbing;
@end
@implementation MediaServiceForStubbing
- (id <MediaServiceRemote>)remoteForBlog:(Blog *)blog
{
return self.remoteForStubbing;
}
@end
@interface MediaServiceUpdateTests : XCTestCase
@property (nonatomic, strong) id<CoreDataStack> manager;
@property (nonatomic, strong) Blog *blog;
@property (nonatomic, strong) MediaServiceForStubbing *service;
@property (nonatomic, strong) NSDate *mediaCreationDate;
@end
@implementation MediaServiceUpdateTests
- (void)setUp
{
[super setUp];
self.manager = [self coreDataStackForTesting];
WordPressComRestApi *api = OCMStrictClassMock([WordPressComRestApi class]);
Blog *blog = [ModelTestHelper insertDotComBlogWithContext:self.manager.mainContext];
blog.account.wordPressComRestApi = api;
blog.dotComID = @1;
self.blog = blog;
MediaServiceForStubbing *service = [[MediaServiceForStubbing alloc] initWithManagedObjectContext:self.manager.mainContext];
MediaServiceRemoteREST *remoteService = OCMStrictClassMock([MediaServiceRemoteREST class]);
service.remoteForStubbing = remoteService;
self.service = service;
self.mediaCreationDate = [NSDate date];
}
- (void)tearDown
{
[super tearDown];
self.blog = nil;
self.service = nil;
self.manager = nil;
self.mediaCreationDate = nil;
}
- (void)testUpdateMediaWorks
{
Media *media = [self generateMedia: @100];
// Check that all media details are passed for updating the media item.
BOOL (^checkRemoteMedia)(id obj) = ^BOOL(RemoteMedia *remoteMedia) {
if (![remoteMedia.mediaID isEqualToNumber:media.mediaID]) {
return NO;
} else if (![remoteMedia.url isEqual:[NSURL URLWithString:media.remoteURL]]) {
return NO;
} else if (![remoteMedia.largeURL isEqual:[NSURL URLWithString:media.remoteLargeURL]]) {
return NO;
} else if (![remoteMedia.mediumURL isEqual:[NSURL URLWithString:media.remoteMediumURL]]) {
return NO;
} else if (![remoteMedia.date isEqualToDate:media.creationDate]) {
return NO;
} else if (![remoteMedia.file isEqualToString:media.filename]) {
return NO;
} else if (![remoteMedia.extension isEqualToString:[media fileExtension]]) {
return NO;
} else if (![remoteMedia.title isEqualToString:media.title]) {
return NO;
} else if (![remoteMedia.caption isEqualToString:media.caption]) {
return NO;
} else if (![remoteMedia.descriptionText isEqualToString:media.desc]) {
return NO;
} else if (![remoteMedia.alt isEqualToString:media.alt]) {
return NO;
} else if (![remoteMedia.height isEqualToNumber:media.height]) {
return NO;
} else if (![remoteMedia.width isEqualToNumber:media.width]) {
return NO;
} else if (![remoteMedia.localURL isEqual:media.absoluteLocalURL]) {
return NO;
} else if (![remoteMedia.mimeType isEqualToString:media.mimeType]) {
return NO;
} else if (![remoteMedia.videopressGUID isEqualToString:media.videopressGUID]) {
return NO;
} else if (![remoteMedia.remoteThumbnailURL isEqualToString:media.remoteThumbnailURL]) {
return NO;
} else if (![remoteMedia.postID isEqualToNumber:media.postID]) {
return NO;
}
return YES;
};
MediaServiceRemoteREST *remote = self.service.remoteForStubbing;
OCMStub([remote updateMedia:[OCMArg checkWithBlock:checkRemoteMedia] success:[OCMArg isNotNil] failure:[OCMArg isNotNil]]);
XCTAssertNoThrow([self.service updateMedia:media success:^{} failure:^(NSError * _Nonnull __unused error) {}]);
}
- (void)testUpdateMultpipleMediaWorks
{
Media *media = [self generateMedia: @100];
// We use the same details except `mediaId` in the defined media items.
NSArray<Media *> *mediaItems = [NSArray arrayWithObjects: media, [self generateMedia:@101], nil];
// Check that all media details are passed for updating the media items.
BOOL (^checkRemoteMedia)(id obj) = ^BOOL(RemoteMedia *remoteMedia) {
if (![remoteMedia.mediaID isEqualToNumber:mediaItems[0].mediaID] &&
![remoteMedia.mediaID isEqualToNumber:mediaItems[1].mediaID])
{
return NO;
} else if (![remoteMedia.url isEqual:[NSURL URLWithString:media.remoteURL]]) {
return NO;
} else if (![remoteMedia.largeURL isEqual:[NSURL URLWithString:media.remoteLargeURL]]) {
return NO;
} else if (![remoteMedia.mediumURL isEqual:[NSURL URLWithString:media.remoteMediumURL]]) {
return NO;
} else if (![remoteMedia.date isEqualToDate:media.creationDate]) {
return NO;
} else if (![remoteMedia.file isEqualToString:media.filename]) {
return NO;
} else if (![remoteMedia.extension isEqualToString:[media fileExtension]]) {
return NO;
} else if (![remoteMedia.title isEqualToString:media.title]) {
return NO;
} else if (![remoteMedia.caption isEqualToString:media.caption]) {
return NO;
} else if (![remoteMedia.descriptionText isEqualToString:media.desc]) {
return NO;
} else if (![remoteMedia.alt isEqualToString:media.alt]) {
return NO;
} else if (![remoteMedia.height isEqualToNumber:media.height]) {
return NO;
} else if (![remoteMedia.width isEqualToNumber:media.width]) {
return NO;
} else if (![remoteMedia.localURL isEqual:media.absoluteLocalURL]) {
return NO;
} else if (![remoteMedia.mimeType isEqualToString:media.mimeType]) {
return NO;
} else if (![remoteMedia.videopressGUID isEqualToString:media.videopressGUID]) {
return NO;
} else if (![remoteMedia.remoteThumbnailURL isEqualToString:media.remoteThumbnailURL]) {
return NO;
} else if (![remoteMedia.postID isEqualToNumber:media.postID]) {
return NO;
}
return YES;
};
MediaServiceRemoteREST *remote = self.service.remoteForStubbing;
OCMStub([remote updateMedia:[OCMArg checkWithBlock:checkRemoteMedia] success:[OCMArg isNotNil] failure:[OCMArg isNotNil]]);
XCTAssertNoThrow([self.service updateMedia:mediaItems overallSuccess:^{} failure:^(NSError * _Nonnull __unused error) {}]);
}
- (void)testUpdateMediaWithSpecificFieldsWorks
{
Media *media = [self generateMedia: @100];
// Check that only `title` and `postID` details are passed for updating the media item.
// NOTE: `mediaID` is always passed as it's mandatory for the update.
BOOL (^checkRemoteMedia)(id obj) = ^BOOL(RemoteMedia *remoteMedia) {
if (![remoteMedia.mediaID isEqualToNumber:media.mediaID]) {
return NO;
} else if (![remoteMedia.title isEqualToString:media.title]) {
return NO;
} else if (![remoteMedia.postID isEqualToNumber:media.postID]) {
return NO;
} else if (remoteMedia.url != nil) {
return NO;
} else if (remoteMedia.largeURL != nil) {
return NO;
} else if (remoteMedia.mediumURL != nil) {
return NO;
} else if (remoteMedia.date != nil) {
return NO;
} else if (remoteMedia.file != nil) {
return NO;
} else if (remoteMedia.extension != nil) {
return NO;
} else if (remoteMedia.caption != nil) {
return NO;
} else if (remoteMedia.descriptionText != nil) {
return NO;
} else if (remoteMedia.alt != nil) {
return NO;
} else if (remoteMedia.height != nil) {
return NO;
} else if (remoteMedia.width != nil) {
return NO;
} else if (remoteMedia.localURL != nil) {
return NO;
} else if (remoteMedia.mimeType != nil) {
return NO;
} else if (remoteMedia.videopressGUID != nil) {
return NO;
} else if (remoteMedia.remoteThumbnailURL != nil) {
return NO;
}
return YES;
};
MediaServiceRemoteREST *remote = self.service.remoteForStubbing;
OCMStub([remote updateMedia:[OCMArg checkWithBlock:checkRemoteMedia] success:[OCMArg isNotNil] failure:[OCMArg isNotNil]]);
NSArray *fieldsToUpdate = [NSArray arrayWithObjects: @"postID", @"title", nil];
XCTAssertNoThrow([self.service updateMedia:media fieldsToUpdate:fieldsToUpdate success:^{} failure:^(NSError * _Nonnull __unused error) {}]);
}
- (void)testUpdateMultpipleMediaWithSpecificFieldsWorks
{
Media *media = [self generateMedia: @100];
// We use the same details except `mediaId` in the defined media items.
NSArray<Media *> *mediaItems = [NSArray arrayWithObjects: media, [self generateMedia:@101], nil];
// Check that only `title` and `postID` details are passed for updating the media item.
// NOTE: `mediaID` is always passed as it's mandatory for the update.
BOOL (^checkRemoteMedia)(id obj) = ^BOOL(RemoteMedia *remoteMedia) {
if (![remoteMedia.mediaID isEqualToNumber:mediaItems[0].mediaID] &&
![remoteMedia.mediaID isEqualToNumber:mediaItems[1].mediaID])
{
return NO;
} else if (![remoteMedia.title isEqualToString:media.title]) {
return NO;
} else if (![remoteMedia.postID isEqualToNumber:media.postID]) {
return NO;
} else if (remoteMedia.url != nil) {
return NO;
} else if (remoteMedia.largeURL != nil) {
return NO;
} else if (remoteMedia.mediumURL != nil) {
return NO;
} else if (remoteMedia.date != nil) {
return NO;
} else if (remoteMedia.file != nil) {
return NO;
} else if (remoteMedia.extension != nil) {
return NO;
} else if (remoteMedia.caption != nil) {
return NO;
} else if (remoteMedia.descriptionText != nil) {
return NO;
} else if (remoteMedia.alt != nil) {
return NO;
} else if (remoteMedia.height != nil) {
return NO;
} else if (remoteMedia.width != nil) {
return NO;
} else if (remoteMedia.localURL != nil) {
return NO;
} else if (remoteMedia.mimeType != nil) {
return NO;
} else if (remoteMedia.videopressGUID != nil) {
return NO;
} else if (remoteMedia.remoteThumbnailURL != nil) {
return NO;
}
return YES;
};
MediaServiceRemoteREST *remote = self.service.remoteForStubbing;
OCMStub([remote updateMedia:[OCMArg checkWithBlock:checkRemoteMedia] success:[OCMArg isNotNil] failure:[OCMArg isNotNil]]);
NSArray *fieldsToUpdate = [NSArray arrayWithObjects: @"postID", @"title", nil];
XCTAssertNoThrow([self.service updateMedia:mediaItems fieldsToUpdate:fieldsToUpdate overallSuccess:^{} failure:^(NSError * _Nonnull __unused error) {}]);
}
/// Helper to generate a Media object with test data.
- (Media *)generateMedia:(NSNumber *)mediaId {
Media *media = [NSEntityDescription
insertNewObjectForEntityForName:[Media entityName]
inManagedObjectContext:self.manager.mainContext];
media.mediaID = mediaId;
media.remoteURL = @"https://wordpress.com/remote";
media.remoteLargeURL = @"https://wordpress.com/remote-large";
media.remoteMediumURL = @"https://wordpress.com/remote-medium";
media.creationDate = self.mediaCreationDate;
media.filename = @"file.png";
media.title = @"Media title";
media.caption = @"Media caption";
media.desc = @"Media description";
media.alt = @"Media alt";
media.height = @150;
media.width = @250;
media.localURL = @"file:///file.png";
media.videopressGUID = @"AbCDe";
media.remoteThumbnailURL = @"https://wordpress.com/remote-thumbnail";
media.postID = @1;
return media;
}
@end