@@ -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 ( ) {
@@ -116,6 +214,33 @@ where
116
214
RestoreServer :: db ( ) . await ;
117
215
}
118
216
217
+ async fn test_create_template < F > ( params : & TemplateCreateParams , assert : F )
218
+ where
219
+ F : Fn ( TemplateWithEditContext ) ,
220
+ {
221
+ let response = api_client ( )
222
+ . templates ( )
223
+ . create ( params)
224
+ . await
225
+ . assert_response ( ) ;
226
+ assert ( response. data ) ;
227
+ RestoreServer :: db ( ) . await ;
228
+ }
229
+
230
+ fn assert_slug ( template : & TemplateWithEditContext ) {
231
+ assert_eq ! ( template. slug, TEST_SLUG ) ;
232
+ }
233
+
234
+ fn assert_title ( template : & TemplateWithEditContext ) {
235
+ assert_eq ! (
236
+ template. title,
237
+ SparseTemplateTitleWrapper :: Object ( SparseTemplateTitle {
238
+ raw: Some ( TEST_TITLE . to_string( ) ) ,
239
+ rendered: Some ( TEST_TITLE . to_string( ) )
240
+ } )
241
+ ) ;
242
+ }
243
+
119
244
mod macro_helper {
120
245
macro_rules! generate_update_test {
121
246
( $ident: ident, $field: ident, $new_value: expr, $assertion: expr) => {
0 commit comments