forked from o1egl/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 9
/
posts_revisions_test.go
161 lines (131 loc) · 4.43 KB
/
posts_revisions_test.go
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
package wordpress_test
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/robbiet480/go-wordpress"
)
func getLatestRevisionForPost(t *testing.T, ctx context.Context, post *wordpress.Post) *wordpress.Revision {
revisions, resp, _ := post.Revisions().List(ctx, nil)
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if len(revisions) < 1 {
t.Fatalf("Should not return empty revisions")
}
// get latest revision
revisionID := revisions[0].ID
revision, resp, _ := post.Revisions().Get(ctx, revisionID, nil)
if resp != nil && resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK, got %v", resp.Status)
}
return revision
}
func TestPostsRevisions_InvalidCall(t *testing.T) {
// User is not allowed to call create wordpress.Post object manually to retrieve PostMetaService
// A proper API call would inject the right PostMetaService, Client and other goodies into a post,
// allowing user to call post.Revisions()
invalidPost := wordpress.Post{}
invalidRevisions := invalidPost.Revisions()
if invalidRevisions != nil {
t.Errorf("Expected revisions to be nil, %v", invalidRevisions)
}
}
func TestPostsRevisionsList(t *testing.T) {
wp, ctx := initTestClient()
post := getAnyOnePost(t, ctx, wp)
revisions, resp, err := post.Revisions().List(ctx, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if revisions == nil {
t.Errorf("Should not return nil revisions")
}
}
func TestPostsRevisionsList_Lazy(t *testing.T) {
wp, ctx := initTestClient()
post := getAnyOnePost(t, ctx, wp)
postID := post.ID
// Use Posts.Entity(postID) to retrieve revisions in one API call
lazyRevisions, resp, err := wp.Posts.Entity(postID).Revisions().List(ctx, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if lazyRevisions == nil {
t.Errorf("Should not return nil revisions")
}
}
func TestPostsRevisionsGet(t *testing.T) {
wp, ctx := initTestClient()
post := getAnyOnePost(t, ctx, wp)
r := getLatestRevisionForPost(t, ctx, post)
revisionID := r.ID
revision, resp, err := post.Revisions().Get(ctx, revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if revision == nil {
t.Errorf("Should not return nil revisions")
}
}
func TestPostsRevisionsGet_Lazy(t *testing.T) {
wp, ctx := initTestClient()
post := getAnyOnePost(t, ctx, wp)
r := getLatestRevisionForPost(t, ctx, post)
postID := post.ID
revisionID := r.ID
// Use Posts.Entity(postID) to retrieve revisions in one API call
lazyRevision, resp, err := wp.Posts.Entity(postID).Revisions().Get(ctx, revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if lazyRevision == nil {
t.Errorf("Should not return nil revisions")
}
}
func TestPostsRevisionsDelete_Lazy(t *testing.T) {
wp, ctx := initTestClient()
post := getAnyOnePost(t, ctx, wp)
// Edit post to create a new revision
// Note: wordpress would only create a new revision if there is an actual change in
// content
now := time.Now()
originalTitle := post.Title.Raw
post.Title.Raw = fmt.Sprintf("%v", now.Format("20060102150405"))
if originalTitle == post.Title.Raw {
t.Fatalf("Flawed test, ensure that post content is modified before an update")
}
updatedPost, resp, _ := wp.Posts.Update(ctx, post.ID, post)
if resp != nil && resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK, got %v", resp.Status)
}
r := getLatestRevisionForPost(t, ctx, updatedPost)
postID := updatedPost.ID
revisionID := r.ID
// Use Posts.Entity(postID) to delete revisions in one API call
// Note that deleting a revision does NOT reverse the changes made in the revision
response, resp, err := wp.Posts.Entity(postID).Revisions().Delete(ctx, revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp != nil && resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if response == nil {
t.Errorf("Should not return nil response")
}
}