Code Duplication    Length = 6-10 lines in 27 locations

tests/phpunit/tests/rest-api/rest-categories-controller.php 6 locations

@@ 94-100 (lines=7) @@
91
        $this->check_get_taxonomy_terms_response($response);
92
    }
93
94
    public function test_get_items_invalid_permission_for_context() 
95
    {
96
        wp_set_current_user(0);
97
        $request = new WP_REST_Request('GET', '/wp/v2/categories');
98
        $request->set_param('context', 'edit');
99
        $response = $this->server->dispatch($request);
100
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
101
    }
102
103
    public function test_get_items_hide_empty_arg() 
@@ 625-631 (lines=7) @@
622
        $this->assertErrorResponse('rest_term_invalid', $response, 404);
623
    }
624
625
    public function test_get_item_invalid_permission_for_context() 
626
    {
627
        wp_set_current_user(0);
628
        $request = new WP_REST_Request('GET', '/wp/v2/categories/1');
629
        $request->set_param('context', 'edit');
630
        $response = $this->server->dispatch($request);
631
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
632
    }
633
634
    public function test_get_term_private_taxonomy() 
@@ 670-676 (lines=7) @@
667
        $this->assertEquals('so-awesome', $data['slug']);
668
    }
669
670
    public function test_create_item_invalid_taxonomy() 
671
    {
672
        wp_set_current_user(self::$administrator);
673
        $request = new WP_REST_Request('POST', '/wp/v2/invalid-taxonomy');
674
        $request->set_param('name', 'Invalid Taxonomy');
675
        $response = $this->server->dispatch($request);
676
        $this->assertErrorResponse('rest_no_route', $response, 404);
677
    }
678
679
    public function test_create_item_incorrect_permissions() 
@@ 679-685 (lines=7) @@
676
        $this->assertErrorResponse('rest_no_route', $response, 404);
677
    }
678
679
    public function test_create_item_incorrect_permissions() 
680
    {
681
        wp_set_current_user(self::$subscriber);
682
        $request = new WP_REST_Request('POST', '/wp/v2/categories');
683
        $request->set_param('name', 'Incorrect permissions');
684
        $response = $this->server->dispatch($request);
685
        $this->assertErrorResponse('rest_cannot_create', $response, 403);
686
    }
687
688
    public function test_create_item_missing_arguments() 
@@ 742-748 (lines=7) @@
739
        $this->assertEquals('new-slug', $data['slug']);
740
    }
741
742
    public function test_update_item_invalid_taxonomy() 
743
    {
744
        wp_set_current_user(self::$administrator);
745
        $request = new WP_REST_Request('POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
746
        $request->set_param('name', 'Invalid Taxonomy');
747
        $response = $this->server->dispatch($request);
748
        $this->assertErrorResponse('rest_no_route', $response, 404);
749
    }
750
751
    public function test_update_item_invalid_term() 
@@ 751-757 (lines=7) @@
748
        $this->assertErrorResponse('rest_no_route', $response, 404);
749
    }
750
751
    public function test_update_item_invalid_term() 
752
    {
753
        wp_set_current_user(self::$administrator);
754
        $request = new WP_REST_Request('POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
755
        $request->set_param('name', 'Invalid Term');
756
        $response = $this->server->dispatch($request);
757
        $this->assertErrorResponse('rest_term_invalid', $response, 404);
758
    }
759
760
    public function test_update_item_incorrect_permissions() 

tests/phpunit/tests/rest-api/rest-comments-controller.php 4 locations

@@ 385-391 (lines=7) @@
382
        wp_delete_comment($comment_id);
383
    }
384
385
    public function test_get_items_no_permission_for_context() 
386
    {
387
        wp_set_current_user(0);
388
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
389
        $request->set_param('context', 'edit');
390
        $response = $this->server->dispatch($request);
391
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
392
    }
393
394
    public function test_get_items_no_post() 
@@ 406-412 (lines=7) @@
403
        $this->assertCount(2, $comments);
404
    }
405
406
    public function test_get_items_no_permission_for_no_post() 
407
    {
408
        wp_set_current_user(0);
409
        $request = new WP_REST_Request('GET', '/wp/v2/comments');
410
        $request->set_param('post', 0);
411
        $response = $this->server->dispatch($request);
412
        $this->assertErrorResponse('rest_cannot_read', $response, 401);
413
    }
414
415
    public function test_get_items_edit_context() 
@@ 904-910 (lines=7) @@
901
        $this->assertEquals(substr(get_avatar_url($comment->comment_author_email), 9), substr($data['author_avatar_urls'][96], 9));
902
    }
903
904
    public function test_get_comment_invalid_id() 
905
    {
906
        $request = new WP_REST_Request('GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
907
908
        $response = $this->server->dispatch($request);
909
        $this->assertErrorResponse('rest_comment_invalid_id', $response, 404);
910
    }
911
912
    public function test_get_comment_invalid_context() 
913
    {
@@ 912-918 (lines=7) @@
909
        $this->assertErrorResponse('rest_comment_invalid_id', $response, 404);
910
    }
911
912
    public function test_get_comment_invalid_context() 
913
    {
914
        wp_set_current_user(0);
915
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%s', self::$approved_id));
916
        $request->set_param('context', 'edit');
917
        $response = $this->server->dispatch($request);
918
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
919
    }
920
921
    public function test_get_comment_invalid_post_id() 

tests/phpunit/tests/rest-api/rest-post-statuses-controller.php 1 location

@@ 96-101 (lines=6) @@
93
        $this->assertErrorResponse('rest_status_invalid', $response, 404);
94
    }
95
96
    public function test_get_item_invalid_access() 
97
    {
98
        wp_set_current_user(0);
99
        $request = new WP_REST_Request('GET', '/wp/v2/statuses/draft');
100
        $response = $this->server->dispatch($request);
101
        $this->assertErrorResponse('rest_cannot_read_status', $response, 401);
102
    }
103
104
    public function test_get_item_invalid_internal() 

tests/phpunit/tests/rest-api/rest-post-types-controller.php 2 locations

@@ 53-59 (lines=7) @@
50
        $this->assertFalse(isset($data['revision']));
51
    }
52
53
    public function test_get_items_invalid_permission_for_context() 
54
    {
55
        wp_set_current_user(0);
56
        $request = new WP_REST_Request('GET', '/wp/v2/types');
57
        $request->set_param('context', 'edit');
58
        $response = $this->server->dispatch($request);
59
        $this->assertErrorResponse('rest_cannot_view', $response, 401);
60
    }
61
62
    public function test_get_item() 
@@ 97-103 (lines=7) @@
94
        $this->check_post_type_object_response('edit', $response);
95
    }
96
97
    public function test_get_item_invalid_permission_for_context() 
98
    {
99
        wp_set_current_user(0);
100
        $request = new WP_REST_Request('GET', '/wp/v2/types/post');
101
        $request->set_param('context', 'edit');
102
        $response = $this->server->dispatch($request);
103
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
104
    }
105
106
    public function test_create_item() 

tests/phpunit/tests/rest-api/rest-posts-controller.php 4 locations

@@ 427-433 (lines=7) @@
424
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
425
    }
426
427
    public function test_get_items_invalid_status_query() 
428
    {
429
        wp_set_current_user(0);
430
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
431
        $request->set_param('status', 'invalid');
432
        $response = $this->server->dispatch($request);
433
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
434
    }
435
436
    public function test_get_items_status_without_permissions() 
@@ 1008-1014 (lines=7) @@
1005
        $this->assertErrorResponse('rest_forbidden', $response, 403);
1006
    }
1007
1008
    public function test_get_post_invalid_id() 
1009
    {
1010
        $request = new WP_REST_Request('GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
1011
        $response = $this->server->dispatch($request);
1012
1013
        $this->assertErrorResponse('rest_post_invalid_id', $response, 404);
1014
    }
1015
1016
    public function test_get_post_list_context_with_permission() 
1017
    {
@@ 1032-1041 (lines=10) @@
1029
        $this->check_get_posts_response($response, 'edit');
1030
    }
1031
1032
    public function test_get_post_list_context_without_permission() 
1033
    {
1034
        wp_set_current_user(0);
1035
        $request = new WP_REST_Request('GET', '/wp/v2/posts');
1036
        $request->set_query_params(
1037
            array(
1038
            'context' => 'edit',
1039
            ) 
1040
        );
1041
        $response = $this->server->dispatch($request);
1042
1043
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
1044
    }
@@ 1046-1055 (lines=10) @@
1043
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
1044
    }
1045
1046
    public function test_get_post_context_without_permission() 
1047
    {
1048
        wp_set_current_user(0);
1049
        $request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', self::$post_id));
1050
        $request->set_query_params(
1051
            array(
1052
            'context' => 'edit',
1053
            ) 
1054
        );
1055
        $response = $this->server->dispatch($request);
1056
1057
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
1058
    }

tests/phpunit/tests/rest-api/rest-settings-controller.php 3 locations

@@ 302-308 (lines=7) @@
299
        remove_all_filters('rest_pre_update_setting');
300
    }
301
302
    public function test_update_item_with_invalid_type() 
303
    {
304
        wp_set_current_user(self::$administrator);
305
        $request = new WP_REST_Request('PUT', '/wp/v2/settings');
306
        $request->set_param('title', array( 'rendered' => 'This should fail.' ));
307
        $response = $this->server->dispatch($request);
308
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
309
    }
310
311
    public function test_update_item_with_integer() 
@@ 320-326 (lines=7) @@
317
        $this->assertEquals(200, $response->get_status());
318
    }
319
320
    public function test_update_item_with_invalid_float_for_integer() 
321
    {
322
        wp_set_current_user(self::$administrator);
323
        $request = new WP_REST_Request('PUT', '/wp/v2/settings');
324
        $request->set_param('posts_per_page', 10.5);
325
        $response = $this->server->dispatch($request);
326
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
327
    }
328
329
    /**
@@ 346-354 (lines=9) @@
343
        $this->assertEquals(10, $data['posts_per_page']);
344
    }
345
346
    public function test_update_item_with_invalid_enum() 
347
    {
348
        update_option('posts_per_page', 9);
349
350
        wp_set_current_user(self::$administrator);
351
        $request = new WP_REST_Request('PUT', '/wp/v2/settings');
352
        $request->set_param('default_ping_status', 'open&closed');
353
        $response = $this->server->dispatch($request);
354
        $this->assertErrorResponse('rest_invalid_param', $response, 400);
355
    }
356
357
    public function test_update_item_with_invalid_stored_value_in_options() 

tests/phpunit/tests/rest-api/rest-tags-controller.php 3 locations

@@ 110-116 (lines=7) @@
107
        $this->check_get_taxonomy_terms_response($response);
108
    }
109
110
    public function test_get_items_invalid_permission_for_context() 
111
    {
112
        wp_set_current_user(0);
113
        $request = new WP_REST_Request('GET', '/wp/v2/tags');
114
        $request->set_param('context', 'edit');
115
        $response = $this->server->dispatch($request);
116
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
117
    }
118
119
    public function test_get_items_hide_empty_arg() 
@@ 567-573 (lines=7) @@
564
        $this->assertEquals('so-awesome', $data['slug']);
565
    }
566
567
    public function test_create_item_incorrect_permissions() 
568
    {
569
        wp_set_current_user(self::$subscriber);
570
        $request = new WP_REST_Request('POST', '/wp/v2/tags');
571
        $request->set_param('name', 'Incorrect permissions');
572
        $response = $this->server->dispatch($request);
573
        $this->assertErrorResponse('rest_cannot_create', $response, 403);
574
    }
575
576
    public function test_create_item_missing_arguments() 
@@ 636-642 (lines=7) @@
633
        $this->assertEquals(200, $response->get_status());
634
    }
635
636
    public function test_update_item_invalid_term() 
637
    {
638
        wp_set_current_user(self::$administrator);
639
        $request = new WP_REST_Request('POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER);
640
        $request->set_param('name', 'Invalid Term');
641
        $response = $this->server->dispatch($request);
642
        $this->assertErrorResponse('rest_term_invalid', $response, 404);
643
    }
644
645
    public function test_update_item_incorrect_permissions() 

tests/phpunit/tests/rest-api/rest-taxonomies-controller.php 3 locations

@@ 55-61 (lines=7) @@
52
        $this->assertEquals('tags', $data['post_tag']['rest_base']);
53
    }
54
55
    public function test_get_items_invalid_permission_for_context() 
56
    {
57
        wp_set_current_user(0);
58
        $request = new WP_REST_Request('GET', '/wp/v2/taxonomies');
59
        $request->set_param('context', 'edit');
60
        $response = $this->server->dispatch($request);
61
        $this->assertErrorResponse('rest_cannot_view', $response, 401);
62
    }
63
64
    public function test_get_taxonomies_for_type() 
@@ 99-105 (lines=7) @@
96
        $this->check_taxonomy_object_response('edit', $response);
97
    }
98
99
    public function test_get_item_invalid_permission_for_context() 
100
    {
101
        wp_set_current_user(0);
102
        $request = new WP_REST_Request('GET', '/wp/v2/taxonomies/category');
103
        $request->set_param('context', 'edit');
104
        $response = $this->server->dispatch($request);
105
        $this->assertErrorResponse('rest_forbidden_context', $response, 401);
106
    }
107
108
    public function test_get_invalid_taxonomy() 
@@ 115-121 (lines=7) @@
112
        $this->assertErrorResponse('rest_taxonomy_invalid', $response, 404);
113
    }
114
115
    public function test_get_non_public_taxonomy() 
116
    {
117
        register_taxonomy('api-private', 'post', array( 'public' => false ));
118
119
        $request = new WP_REST_Request('GET', '/wp/v2/taxonomies/api-private');
120
        $response = $this->server->dispatch($request);
121
        $this->assertErrorResponse('rest_forbidden', $response, 403);
122
    }
123
124
    public function test_create_item() 

tests/phpunit/tests/rest-api/rest-users-controller.php 1 location

@@ 768-774 (lines=7) @@
765
        $this->assertEquals(rest_url('wp/v2/users/' . self::$user), $links['self'][0]['href']);
766
    }
767
768
    public function test_get_current_user_without_permission() 
769
    {
770
        wp_set_current_user(0);
771
        $request = new WP_REST_Request('GET', '/wp/v2/users/me');
772
        $response = $this->server->dispatch($request);
773
774
        $this->assertErrorResponse('rest_not_logged_in', $response, 401);
775
    }
776
777
    public function test_create_item()