Skip to content

Commit d57b6ea

Browse files
committed
Completed taxonomies
Completed `comments` but unable to test, due to issues with comment creations on the server-side.
1 parent ef15555 commit d57b6ea

File tree

6 files changed

+332
-7
lines changed

6 files changed

+332
-7
lines changed

comments.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package wordpress
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
type Comment struct {
9+
ID int `json:"id"`
10+
AvatarURL string `json:"avatar_url"`
11+
AvatarURLs AvatarURLS `json:"avatar_urls"`
12+
Author int `json:"author"`
13+
AuthorEmail string `json:"author_email"`
14+
AuthorIP string `json:"author_ip"`
15+
AuthorName string `json:"author_name"`
16+
AuthorURL string `json:"author_url"`
17+
AuthorUserAgent string `json:"author_user_agent"`
18+
Content Content `json:"content"`
19+
Date string `json:"date"`
20+
DateGMT string `json:"date_gmt"`
21+
Karma int `json:"karma"`
22+
Link string `json:"link"`
23+
Parent int `json:"parent"`
24+
Post int `json:"post"`
25+
Status string `json:"status"`
26+
Type string `json:"type"`
27+
}
28+
29+
type CommentsCollection struct {
30+
client *Client
31+
url string
32+
}
33+
34+
func (col *CommentsCollection) List(params interface{}) ([]Comment, *http.Response, []byte, error) {
35+
var posts []Comment
36+
resp, body, err := col.client.List(col.url, params, &posts)
37+
return posts, resp, body, err
38+
}
39+
func (col *CommentsCollection) Create(new *Comment) (*Comment, *http.Response, []byte, error) {
40+
var created Comment
41+
resp, body, err := col.client.Create(col.url, new, &created)
42+
return &created, resp, body, err
43+
}
44+
func (col *CommentsCollection) Get(id int, params interface{}) (*Comment, *http.Response, []byte, error) {
45+
var entity Comment
46+
entityURL := fmt.Sprintf("%v/%v", col.url, id)
47+
resp, body, err := col.client.Get(entityURL, params, &entity)
48+
return &entity, resp, body, err
49+
}
50+
51+
func (col *CommentsCollection) Update(id int, post *Comment) (*Comment, *http.Response, []byte, error) {
52+
var updated Comment
53+
entityURL := fmt.Sprintf("%v/%v", col.url, id)
54+
resp, body, err := col.client.Update(entityURL, post, &updated)
55+
return &updated, resp, body, err
56+
}
57+
func (col *CommentsCollection) Delete(id int, params interface{}) (*Comment, *http.Response, []byte, error) {
58+
var deleted Comment
59+
entityURL := fmt.Sprintf("%v/%v", col.url, id)
60+
resp, body, err := col.client.Delete(entityURL, params, &deleted)
61+
return &deleted, resp, body, err
62+
}

comments_test.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package wordpress_test
2+
3+
import (
4+
"github.com/sogko/go-wordpress"
5+
"net/http"
6+
"testing"
7+
"log"
8+
)
9+
10+
func factoryComment(postID int) wordpress.Comment {
11+
return wordpress.Comment{
12+
Post: postID,
13+
Author: 1,
14+
Status: wordpress.CommentStatusApproved,
15+
AuthorName: "go-wordpress",
16+
Content: wordpress.Content{
17+
Raw: "Test Comment",
18+
Rendered: "<p>Test Comment</p>",
19+
},
20+
}
21+
}
22+
23+
func cleanUpComment(t *testing.T, commentID int) {
24+
25+
wp := initTestClient()
26+
deletedComment, resp, body, err := wp.Comments().Delete(commentID, "force=true")
27+
if err != nil {
28+
t.Errorf("Failed to clean up new comment: %v", err.Error())
29+
}
30+
if body == nil {
31+
t.Errorf("body should not be nil")
32+
}
33+
if resp.StatusCode != http.StatusOK {
34+
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
35+
}
36+
if deletedComment.ID != commentID {
37+
t.Errorf("Deleted comment ID should be the same as newly created comment: %v != %v", deletedComment.ID, commentID)
38+
}
39+
}
40+
41+
func getAnyOneComment(t *testing.T, wp *wordpress.Client) *wordpress.Comment {
42+
43+
comments, resp, body, err := wp.Comments().List(nil)
44+
if resp.StatusCode != http.StatusOK {
45+
t.Errorf("Expected 200 OK, got %v", resp.Status)
46+
}
47+
if len(comments) < 1 {
48+
log.Print(err)
49+
log.Print(body)
50+
log.Print(resp)
51+
t.Fatalf("Should not return empty comments")
52+
}
53+
54+
commentID := comments[0].ID
55+
56+
comment, resp, _, _ := wp.Comments().Get(commentID, "context=edit")
57+
if resp.StatusCode != http.StatusOK {
58+
t.Errorf("Expected 200 OK, got %v", resp.Status)
59+
}
60+
return comment
61+
}
62+
63+
func TestCommentsList(t *testing.T) {
64+
wp := initTestClient()
65+
66+
comments, resp, body, err := wp.Comments().List(nil)
67+
if err != nil {
68+
t.Errorf("Should not return error: %v", err.Error())
69+
}
70+
if resp.StatusCode != http.StatusOK {
71+
t.Errorf("Expected 200 OK, got %v", resp.Status)
72+
}
73+
if body == nil {
74+
t.Errorf("Should not return nil body")
75+
}
76+
if comments == nil {
77+
t.Errorf("Should not return nil comments")
78+
}
79+
if len(comments) == 0 {
80+
t.Errorf("Should not return empty comments")
81+
}
82+
}
83+
84+
func TestCommentsGet_CommentExists(t *testing.T) {
85+
wp := initTestClient()
86+
87+
c := getAnyOneComment(t, wp)
88+
89+
comment, resp, body, err := wp.Comments().Get(c.ID, nil)
90+
if err != nil {
91+
t.Errorf("Should not return error: %v", err.Error())
92+
}
93+
if resp.StatusCode != http.StatusOK {
94+
t.Errorf("Expected 200 OK, got %v", resp.Status)
95+
}
96+
if body == nil {
97+
t.Errorf("Should not return nil body")
98+
}
99+
if comment == nil {
100+
t.Errorf("Should not return nil comments")
101+
}
102+
}
103+
104+
func TestCommentsGet_CommentDoesNotExists(t *testing.T) {
105+
wp := initTestClient()
106+
107+
comment, resp, body, err := wp.Comments().Get(-1, nil)
108+
if err == nil {
109+
t.Errorf("Should return error")
110+
}
111+
if resp.StatusCode != http.StatusNotFound {
112+
t.Errorf("Expected 404 Not Found, got %v", resp.Status)
113+
}
114+
if body == nil {
115+
t.Errorf("Should not return nil body")
116+
}
117+
if comment == nil {
118+
t.Errorf("Should not return nil comments")
119+
}
120+
}
121+
122+
func TestCommentsCreate(t *testing.T) {
123+
t.Skipf("[TestCommentsCreate] Skipped: there is an issue with creating comments, server returning empty string")
124+
wp := initTestClient()
125+
126+
p := getAnyOnePost(t, wp)
127+
c := factoryComment(p.ID)
128+
129+
newComment, resp, body, err := wp.Comments().Create(&c)
130+
if err != nil {
131+
t.Errorf("Should not return error: %v", err.Error())
132+
}
133+
if resp.StatusCode != http.StatusCreated {
134+
t.Errorf("Expected 201 Created, got %v", resp.Status)
135+
}
136+
if body == nil {
137+
t.Errorf("Should not return nil body")
138+
}
139+
if newComment == nil {
140+
t.Errorf("Should not return nil newComment")
141+
}
142+
143+
cleanUpComment(t, newComment.ID)
144+
}

endpoints.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ List of WP-API REST endpoints and implementation status
1212

1313
## Comments
1414

15-
- [ ] `GET /comments`
16-
- [ ] `POST /comments`
17-
- [ ] `GET /comments/[id]`
18-
- [ ] `PUT /comments/[id]`
19-
- [ ] `DELETE /comments/[id]`
15+
- [x] `GET /comments`
16+
- [x] `POST /comments` (Untested)
17+
- [x] `GET /comments/[id]`
18+
- [x] `PUT /comments/[id]` (Untested)
19+
- [x] `DELETE /comments/[id]` (Untested)
2020

2121
## Meta
2222

@@ -94,8 +94,8 @@ List of WP-API REST endpoints and implementation status
9494

9595
## Taxonomies
9696

97-
- [ ] `GET /taxonomies`
98-
- [ ] `GET /taxonomies/[slug]`
97+
- [x] `GET /taxonomies`
98+
- [x] `GET /taxonomies/[slug]`
9999

100100
## Terms
101101

@@ -107,6 +107,22 @@ List of WP-API REST endpoints and implementation status
107107

108108
`[tax_base] = "tag" | "category"`
109109

110+
### Tag Terms
111+
112+
- [ ] `GET /terms/tag`
113+
- [ ] `POST /terms/tag]`
114+
- [ ] `GET /terms/tag]/[id]`
115+
- [ ] `PUT /terms/tag]/[id]`
116+
- [ ] `DELETE /terms/tag/[id]`
117+
118+
### Category Terms
119+
120+
- [ ] `GET /terms/category`
121+
- [ ] `POST /terms/category]`
122+
- [ ] `GET /terms/category]/[id]`
123+
- [ ] `PUT /terms/category]/[id]`
124+
- [ ] `DELETE /terms/category/[id]`
125+
110126
## Users
111127

112128
- [ ] `GET /users`

posts.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const (
1717
CommentStatusOpen = "open"
1818
CommentStatusClosed = "closed"
1919

20+
CommentStatusApproved= "approved"
21+
CommentStatusUnapproved= "unapproved"
22+
2023
PingStatusOpen = "open"
2124
PingStatusClosed = "closed"
2225

taxonomies.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package wordpress
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
type Labels struct {
9+
Raw string `json:"raw"`
10+
Rendered string `json:"rendered"`
11+
}
12+
type Taxonomy struct {
13+
Description string `json:"description"`
14+
Hierarchical bool `json:"hierarchical"`
15+
Labels map[string]interface{} `json:"labels"`
16+
Name string `json:"name"`
17+
Slug string `json:"slug"`
18+
ShowCloud bool `json:"show_cloud"`
19+
Types []string `json:"types"`
20+
}
21+
type TaxonomiesCollection struct {
22+
client *Client
23+
url string
24+
}
25+
26+
func (col *TaxonomiesCollection) List(params interface{}) ([]Taxonomy, *http.Response, []byte, error) {
27+
var taxonomies []Taxonomy
28+
resp, body, err := col.client.List(col.url, params, &taxonomies)
29+
return taxonomies, resp, body, err
30+
}
31+
32+
func (col *TaxonomiesCollection) Get(slug string, params interface{}) (*Taxonomy, *http.Response, []byte, error) {
33+
var taxonomy Taxonomy
34+
entityURL := fmt.Sprintf("%v/%v", col.url, slug)
35+
resp, body, err := col.client.Get(entityURL, params, &taxonomy)
36+
return &taxonomy, resp, body, err
37+
}

taxonomies_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package wordpress_test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
)
7+
8+
func TestTaxonomiesList(t *testing.T) {
9+
wp := initTestClient()
10+
11+
taxonomies, resp, body, err := wp.Taxonomies().List(nil)
12+
if err != nil {
13+
t.Errorf("Should not return error: %v", err.Error())
14+
}
15+
if resp.StatusCode != http.StatusOK {
16+
t.Errorf("Expected 200 OK, got %v", resp.Status)
17+
}
18+
if body == nil {
19+
t.Errorf("Should not return nil body")
20+
}
21+
if taxonomies == nil {
22+
t.Errorf("Should not return nil taxonomies")
23+
}
24+
if len(taxonomies) != 2 {
25+
t.Errorf("Should return two taxonomies")
26+
}
27+
}
28+
29+
func TestTaxonomiesGet_TaxonomyExists(t *testing.T) {
30+
wp := initTestClient()
31+
32+
taxonomy, resp, body, err := wp.Taxonomies().Get("post_tag", nil)
33+
if err != nil {
34+
t.Errorf("Should not return error: %v", err.Error())
35+
}
36+
if resp.StatusCode != http.StatusOK {
37+
t.Errorf("Expected 200 OK, got %v", resp.Status)
38+
}
39+
if body == nil {
40+
t.Errorf("Should not return nil body")
41+
}
42+
if taxonomy == nil {
43+
t.Errorf("Should not return nil taxonomies")
44+
}
45+
}
46+
47+
func TestTaxonomiesGet_TaxonomyDoesNotExists(t *testing.T) {
48+
wp := initTestClient()
49+
50+
taxonomy, resp, body, err := wp.Taxonomies().Get("RANDOM", nil)
51+
if err == nil {
52+
t.Errorf("Should return error")
53+
}
54+
if resp.StatusCode != http.StatusNotFound {
55+
t.Errorf("Expected 404 Not Found, got %v", resp.Status)
56+
}
57+
if body == nil {
58+
t.Errorf("Should not return nil body")
59+
}
60+
if taxonomy == nil {
61+
t.Errorf("Should not return nil taxonomies")
62+
}
63+
}

0 commit comments

Comments
 (0)