@@ -2,13 +2,111 @@ use macro_helper::generate_update_test;
2
2
use serial_test:: serial;
3
3
use wp_api:: templates:: {
4
4
SparseTemplateContent , SparseTemplateContentWrapper , SparseTemplateTitle ,
5
- SparseTemplateTitleWrapper , TemplateId , TemplateStatus , TemplateUpdateParams ,
6
- TemplateWithEditContext ,
5
+ SparseTemplateTitleWrapper , TemplateCreateParams , TemplateId , TemplateStatus ,
6
+ TemplateUpdateParams , TemplateWithEditContext ,
7
7
} ;
8
8
use wp_api_integration_tests:: {
9
9
AssertResponse , SECOND_USER_ID , TestCredentials , api_client, backend:: RestoreServer ,
10
10
} ;
11
11
12
+ const TEST_SLUG : & str = "foo_template_slug" ;
13
+ const TEST_TITLE : & str = "foo template title" ;
14
+
15
+ #[ tokio:: test]
16
+ #[ serial]
17
+ async fn create_template_with_slug_and_content ( ) {
18
+ let content = "foo template content" ;
19
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
20
+ params. content = Some ( content. to_string ( ) ) ;
21
+ test_create_template ( & params, |created_template| {
22
+ assert_slug ( & created_template) ;
23
+ assert_eq ! (
24
+ created_template. content,
25
+ SparseTemplateContentWrapper :: Object ( SparseTemplateContent {
26
+ raw: Some ( content. to_string( ) ) ,
27
+ rendered: None ,
28
+ protected: None ,
29
+ block_version: None
30
+ } )
31
+ ) ;
32
+ } )
33
+ . await ;
34
+ }
35
+
36
+ #[ tokio:: test]
37
+ #[ serial]
38
+ async fn create_template_with_slug_and_title ( ) {
39
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
40
+ params. title = Some ( TEST_TITLE . to_string ( ) ) ;
41
+ test_create_template ( & params, |created_template| {
42
+ assert_slug ( & created_template) ;
43
+ assert_title ( & created_template) ;
44
+ } )
45
+ . await ;
46
+ }
47
+
48
+ #[ tokio:: test]
49
+ #[ serial]
50
+ async fn create_template_with_slug_title_and_theme ( ) {
51
+ let theme = "foo template theme" ;
52
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
53
+ params. title = Some ( TEST_TITLE . to_string ( ) ) ;
54
+ params. theme = Some ( theme. to_string ( ) ) ;
55
+ test_create_template ( & params, |created_template| {
56
+ assert_slug ( & created_template) ;
57
+ assert_title ( & created_template) ;
58
+ assert_eq ! ( created_template. theme, theme) ;
59
+ } )
60
+ . await ;
61
+ }
62
+
63
+ #[ tokio:: test]
64
+ #[ serial]
65
+ async fn create_template_with_slug_title_and_author ( ) {
66
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
67
+ params. title = Some ( TEST_TITLE . to_string ( ) ) ;
68
+ params. author = Some ( SECOND_USER_ID ) ;
69
+ test_create_template ( & params, |created_template| {
70
+ assert_title ( & created_template) ;
71
+ assert_eq ! ( created_template. author, SECOND_USER_ID ) ;
72
+ } )
73
+ . await ;
74
+ }
75
+
76
+ #[ tokio:: test]
77
+ #[ serial]
78
+ // TODO: `template_type` parameter doesn't seem to be working. It either requires the template type
79
+ // to be set up in advance or the parameter is always ignored.
80
+ #[ ignore]
81
+ async fn create_template_with_slug_title_and_template_type ( ) {
82
+ let template_type = "foo template type" ;
83
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
84
+ params. title = Some ( TEST_TITLE . to_string ( ) ) ;
85
+ params. template_type = Some ( template_type. to_string ( ) ) ;
86
+ test_create_template ( & params, |created_template| {
87
+ assert_title ( & created_template) ;
88
+ assert_eq ! ( created_template. template_type, template_type) ;
89
+ } )
90
+ . await ;
91
+ }
92
+
93
+ #[ tokio:: test]
94
+ #[ serial]
95
+ // TODO: `status` parameter seems to be ignored as the server is always responding with
96
+ // `TemplateStatus::Publish`
97
+ #[ ignore]
98
+ async fn create_template_with_slug_title_and_template_status_future ( ) {
99
+ let status = TemplateStatus :: Future ;
100
+ let mut params = TemplateCreateParams :: new ( TEST_SLUG . to_string ( ) ) ;
101
+ params. title = Some ( TEST_TITLE . to_string ( ) ) ;
102
+ params. status = Some ( status. clone ( ) ) ;
103
+ test_create_template ( & params, |created_template| {
104
+ assert_title ( & created_template) ;
105
+ assert_eq ! ( created_template. status, status) ;
106
+ } )
107
+ . await ;
108
+ }
109
+
12
110
#[ tokio:: test]
13
111
#[ serial]
14
112
async fn delete_template ( ) {
@@ -73,15 +171,9 @@ generate_update_test!(
73
171
generate_update_test ! (
74
172
update_title,
75
173
title,
76
- "new_title" . to_string( ) ,
174
+ TEST_TITLE . to_string( ) ,
77
175
|updated_template| {
78
- assert_eq!(
79
- updated_template. title,
80
- SparseTemplateTitleWrapper :: Object ( SparseTemplateTitle {
81
- raw: Some ( "new_title" . to_string( ) ) ,
82
- rendered: Some ( "new_title" . to_string( ) )
83
- } )
84
- ) ;
176
+ assert_title( & updated_template) ;
85
177
}
86
178
) ;
87
179
generate_update_test ! (
@@ -116,6 +208,33 @@ where
116
208
RestoreServer :: db ( ) . await ;
117
209
}
118
210
211
+ async fn test_create_template < F > ( params : & TemplateCreateParams , assert : F )
212
+ where
213
+ F : Fn ( TemplateWithEditContext ) ,
214
+ {
215
+ let response = api_client ( )
216
+ . templates ( )
217
+ . create ( params)
218
+ . await
219
+ . assert_response ( ) ;
220
+ assert ( response. data ) ;
221
+ RestoreServer :: db ( ) . await ;
222
+ }
223
+
224
+ fn assert_slug ( template : & TemplateWithEditContext ) {
225
+ assert_eq ! ( template. slug, TEST_SLUG ) ;
226
+ }
227
+
228
+ fn assert_title ( template : & TemplateWithEditContext ) {
229
+ assert_eq ! (
230
+ template. title,
231
+ SparseTemplateTitleWrapper :: Object ( SparseTemplateTitle {
232
+ raw: Some ( TEST_TITLE . to_string( ) ) ,
233
+ rendered: Some ( TEST_TITLE . to_string( ) )
234
+ } )
235
+ ) ;
236
+ }
237
+
119
238
mod macro_helper {
120
239
macro_rules! generate_update_test {
121
240
( $ident: ident, $field: ident, $new_value: expr, $assertion: expr) => {
0 commit comments