forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
posts_revisions_test.go
169 lines (145 loc) · 4.54 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
162
163
164
165
166
167
168
169
package wordpress_test
import (
"fmt"
"github.com/sogko/go-wordpress"
"net/http"
"testing"
"time"
)
func getLatestRevisionForPost(t *testing.T, post *wordpress.Post) *wordpress.Revision {
revisions, resp, _, _ := post.Revisions().List(nil)
if 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(revisionID, nil)
if 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 PostMetaCollection
// A proper API call would inject the right PostMetaCollection, Client and other goodies into a post,
// allowing user to call post.Revisions()
invalidPost := wordpress.Post{}
invalidRevisions := invalidPost.Revisions()
if invalidRevisions != nil {
t.Error("Expected revisions to be nil, %v", invalidRevisions)
}
}
func TestPostsRevisionsList(t *testing.T) {
wp := initTestClient()
post := getAnyOnePost(t, wp)
revisions, resp, body, err := post.Revisions().List(nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if 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 := initTestClient()
post := getAnyOnePost(t, wp)
postID := post.ID
// Use Posts().Entity(postID) to retrieve revisions in one API call
lazyRevisions, resp, body, err := wp.Posts().Entity(postID).Revisions().List(nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if 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 := initTestClient()
post := getAnyOnePost(t, wp)
r := getLatestRevisionForPost(t, post)
revisionID := r.ID
revision, resp, body, err := post.Revisions().Get(revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if 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 := initTestClient()
post := getAnyOnePost(t, wp)
r := getLatestRevisionForPost(t, post)
postID := post.ID
revisionID := r.ID
// Use Posts().Entity(postID) to retrieve revisions in one API call
lazyRevision, resp, body, err := wp.Posts().Entity(postID).Revisions().Get(revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if 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 := initTestClient()
post := getAnyOnePost(t, 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(post.ID, post)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK, got %v", resp.Status)
}
r := getLatestRevisionForPost(t, 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, body, err := wp.Posts().Entity(postID).Revisions().Delete(revisionID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if body == nil {
t.Errorf("Should not return nil body")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if response == false {
t.Errorf("Should not return false (bool) response")
}
}