forked from GlotPress/GlotPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_route_translation_set.php
176 lines (149 loc) · 6.58 KB
/
test_route_translation_set.php
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
170
171
172
173
174
175
176
<?php
class GP_Test_Route_Translation_Set extends GP_UnitTestCase_Route {
var $route_class = 'GP_Route_Translation_Set';
function setUp(): void {
parent::setUp();
$this->set = $this->factory->translation_set->create_with_project_and_locale();
}
function test_single_with_a_non_existent_set_gives_404() {
$this->route->single( 11123123 );
$this->assert404();
}
function test_single_redirects_to_existing_set() {
$set = $this->factory->translation_set->create_with_project_and_locale( array( 'slug' => 'baba' ) );
$this->route->single( $set->id );
$this->assertRedirectURLContains( 'baba' );
}
function test_new_get_forbidden_redirect_if_not_logged_in() {
$this->route->new_get();
$this->assertNotAllowedRedirect();
}
function test_new_get_forbidden_redirect_if_logged_in_but_without_sufficient_permissions() {
$this->set_normal_user_as_current();
$this->route->new_get();
$this->assertNotAllowedRedirect();
}
function test_new_get_admin_can_view_the_form() {
$this->set_admin_user_as_current();
$this->route->new_get();
$this->assertTemplateLoadedIs( 'translation-set-new' );
$this->assertTemplateOutputNotEmpty();
}
function test_new_post_invalid_nonce_redirect_if_not_logged_in() {
$this->route->new_post();
$this->assertInvalidNonceRedirect();
}
function test_new_post_forbidden_redirect_if_logged_in_but_without_sufficient_permissions() {
$this->set_normal_user_as_current();
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'add-translation-set' );
$this->route->new_post();
$this->assertNotAllowedRedirect();
}
function test_new_post_invalid_redirect_if_empty_set() {
$this->set_admin_user_as_current();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'add-translation-set' );
$this->route->new_post();
$this->assertInvalidRedirect();
}
function test_new_post_error_redirect_if_create_was_unsuccessful() {
$this->set_admin_user_as_current();
GP::$translation_set = $this->getMockBuilder('GP_Translation_Set')->setMethods(array('create_and_select'))->getMock();
GP::$translation_set->expects( $this->any() )->method( 'create_and_select' );
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'add-translation-set' );
$this->route->new_post();
$this->assertErrorRedirect();
}
function test_new_post_should_add_notice_if_create_was_successful() {
$this->set_admin_user_as_current();
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'add-translation-set' );
$this->route->new_post();
$this->assertThereIsANoticeContaining( 'created' );
}
function test_edit_get_forbidden_redirect_if_not_logged_in() {
$this->route->edit_get( $this->set->id );
$this->assertNotAllowedRedirect();
}
function test_edit_get_forbidden_redirect_if_logged_in_but_without_sufficient_permissions() {
$this->set_normal_user_as_current();
$this->route->edit_get( $this->set->id );
$this->assertNotAllowedRedirect();
}
function test_edit_get_admin_can_view_the_form() {
$this->set_admin_user_as_current();
$this->route->edit_get( $this->set->id );
$this->assertTemplateLoadedIs( 'translation-set-edit' );
$this->assertTemplateOutputNotEmpty();
}
function test_edit_post_with_a_non_existent_set_gives_404() {
$this->set_admin_user_as_current();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_11' );
$this->route->edit_post( 11 );
$this->assert404();
}
function test_edit_post_invalid_nonce_redirect_if_not_logged_in() {
$this->route->edit_post( $this->set->id );
$this->assertInvalidNonceRedirect();
}
function test_edit_post_forbidden_redirect_if_logged_in_but_without_sufficient_permissions() {
$this->set_normal_user_as_current();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->assertNotAllowedRedirect();
}
function test_edit_post_invalid_redirect_if_empty_set() {
$this->set_admin_user_as_current();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->assertInvalidRedirect();
}
function test_edit_post_invalid_redirect_if_missing_name() {
$this->set_admin_user_as_current();
$_POST['set'] = $this->factory->translation_set->generate_args();
unset( $_POST['set']['name'] );
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->assertInvalidRedirect();
}
function test_edit_post_should_add_notice_if_update_was_successful() {
$this->set_admin_user_as_current();
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->assertThereIsANoticeContaining( 'updated' );
}
function test_edit_post_error_redirect_if_create_was_unsuccessful() {
$this->set_admin_user_as_current();
GP::$translation_set = $this->getMockBuilder( 'GP_Translation_Set' )->setMethods( array( 'update' ) )->getMock();
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->assertErrorRedirect();
}
function test_edit_post_the_set_name_should_be_updated() {
$this->set_admin_user_as_current();
$_POST['set'] = $this->factory->translation_set->generate_args();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'edit-translation-set_' . $this->set->id );
$this->route->edit_post( $this->set->id );
$this->set->reload();
$this->assertEquals( $_POST['set']['name'], $this->set->name );
}
function test_delete_post_with_a_non_existent_set_gives_404() {
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'delete-translation-set_11' );
$this->route->delete_post( 11 );
$this->assert404();
}
function test_delete_post_forbidden_redirect_if_not_logged_in() {
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'delete-translation-set_' . $this->set->id );
$this->route->delete_post( $this->set->id );
$this->assertNotAllowedRedirect();
}
function test_delete_post_forbidden_redirect_if_logged_in_but_without_sufficient_permissions() {
$this->set_normal_user_as_current();
$_REQUEST['_gp_route_nonce'] = wp_create_nonce( 'delete-translation-set_' . $this->set->id );
$this->route->delete_post( $this->set->id );
$this->assertNotAllowedRedirect();
}
}