forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pages_revisions_test.go
169 lines (145 loc) · 4.54 KB
/
pages_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 getLatestRevisionForPage(t *testing.T, page *wordpress.Page) *wordpress.Revision {
revisions, resp, _, _ := page.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, _, _ := page.Revisions().Get(revisionID, nil)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK, got %v", resp.Status)
}
return revision
}
func TestPagesRevisions_InvalidCall(t *testing.T) {
// User is not allowed to call create wordpress.Page object manually to retrieve PageMetaCollection
// A proper API call would inject the right PageMetaCollection, Client and other goodies into a page,
// allowing user to call page.Revisions()
invalidPage := wordpress.Page{}
invalidRevisions := invalidPage.Revisions()
if invalidRevisions != nil {
t.Error("Expected revisions to be nil, %v", invalidRevisions)
}
}
func TestPagesRevisionsList(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
revisions, resp, body, err := page.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 TestPagesRevisionsList_Lazy(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
pageID := page.ID
// Use Pages().Entity(pageID) to retrieve revisions in one API call
lazyRevisions, resp, body, err := wp.Pages().Entity(pageID).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 TestPagesRevisionsGet(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
r := getLatestRevisionForPage(t, page)
revisionID := r.ID
revision, resp, body, err := page.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 TestPagesRevisionsGet_Lazy(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
r := getLatestRevisionForPage(t, page)
pageID := page.ID
revisionID := r.ID
// Use Pages().Entity(pageID) to retrieve revisions in one API call
lazyRevision, resp, body, err := wp.Pages().Entity(pageID).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 TestPagesRevisionsDelete_Lazy(t *testing.T) {
wp := initTestClient()
page := getAnyOnePage(t, wp)
// Edit page 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 := page.Title.Raw
page.Title.Raw = fmt.Sprintf("%v", now.Format("20060102150405"))
if originalTitle == page.Title.Raw {
t.Fatalf("Flawed test, ensure that page content is modified before an update")
}
updatedPage, resp, _, _ := wp.Pages().Update(page.ID, page)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 OK, got %v", resp.Status)
}
r := getLatestRevisionForPage(t, updatedPage)
pageID := updatedPage.ID
revisionID := r.ID
// Use Pages().Entity(pageID) 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.Pages().Entity(pageID).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")
}
}