|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + * @package MaterialDesign |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Test_Block_Editor |
| 22 | + * |
| 23 | + * @package MaterialDesign |
| 24 | + */ |
| 25 | + |
| 26 | +namespace MaterialDesign\Test\Theme\BlockEditor; |
| 27 | + |
| 28 | +use MaterialDesign\Theme\BlockEditor; |
| 29 | + |
| 30 | +/** |
| 31 | + * Class Test_Block_Editor |
| 32 | + * |
| 33 | + * @package MaterialDesign |
| 34 | + */ |
| 35 | +class Test_Block_Editor extends \WP_UnitTestCase { |
| 36 | + |
| 37 | + /** |
| 38 | + * Post id. |
| 39 | + * |
| 40 | + * @var int |
| 41 | + */ |
| 42 | + protected static $post_id; |
| 43 | + |
| 44 | + /** |
| 45 | + * Post id. |
| 46 | + * |
| 47 | + * @var int |
| 48 | + */ |
| 49 | + protected static $cpt_post_id; |
| 50 | + |
| 51 | + /** |
| 52 | + * Backed up meta keys. |
| 53 | + * |
| 54 | + * @var array |
| 55 | + */ |
| 56 | + protected static $wp_meta_keys_saved; |
| 57 | + |
| 58 | + /** |
| 59 | + * Setup. |
| 60 | + * |
| 61 | + * @param \WP_UnitTest_Factory $factory Factory method. |
| 62 | + */ |
| 63 | + public static function wpSetUpBeforeClass( $factory ) { |
| 64 | + self::$post_id = $factory->post->create( [ 'post_type' => 'page' ] ); |
| 65 | + register_post_type( |
| 66 | + 'cpt', |
| 67 | + [ |
| 68 | + 'show_in_rest' => true, |
| 69 | + 'supports' => [ 'custom-fields' ], |
| 70 | + ] |
| 71 | + ); |
| 72 | + self::$cpt_post_id = $factory->post->create( [ 'post_type' => 'cpt' ] ); |
| 73 | + self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : []; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tear down. |
| 78 | + */ |
| 79 | + public static function wpTearDownAfterClass() { |
| 80 | + $GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved; |
| 81 | + wp_delete_post( self::$post_id, true ); |
| 82 | + wp_delete_post( self::$cpt_post_id, true ); |
| 83 | + |
| 84 | + unregister_post_type( 'cpt' ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test init. |
| 89 | + */ |
| 90 | + public function test_init() { |
| 91 | + $this->assertEquals( 10, has_action( 'init', 'MaterialDesign\\Theme\\BlockEditor\\register_disable_section_meta' ) ); |
| 92 | + $this->assertEquals( 10, has_action( 'enqueue_block_editor_assets', 'MaterialDesign\\Theme\\BlockEditor\\enqueue_block_editor_assets' ) ); |
| 93 | + $this->assertEquals( 10, has_action( 'body_class', 'MaterialDesign\\Theme\\BlockEditor\\filter_body_class' ) ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test enqueue_block_editor_assets(). |
| 98 | + * |
| 99 | + * @see enqueue_block_editor_assets() |
| 100 | + */ |
| 101 | + public function test_enqueue_block_editor_assets() { |
| 102 | + BlockEditor\enqueue_block_editor_assets(); |
| 103 | + $this->assertTrue( wp_script_is( 'material-block-editor-js-theme' ) ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test register_disable_section_meta. |
| 108 | + */ |
| 109 | + public function test_register_disable_section_meta() { |
| 110 | + global $wp_meta_keys; |
| 111 | + BlockEditor\register_disable_section_meta(); |
| 112 | + $this->assertIsArray( $wp_meta_keys['post']['']['material-hide-sections'] ); |
| 113 | + // Ensure no data exists currently. |
| 114 | + $values = get_post_meta( self::$post_id, 'material-hide-sections', true ); |
| 115 | + $this->assertEmpty( $values ); |
| 116 | + |
| 117 | + $this->grant_write_permission(); |
| 118 | + |
| 119 | + $data = [ |
| 120 | + 'meta' => [ |
| 121 | + 'material-hide-sections' => [ 'title' => 1 ], |
| 122 | + ], |
| 123 | + ]; |
| 124 | + |
| 125 | + $map = [ |
| 126 | + 'pages' => self::$post_id, |
| 127 | + 'cpt' => self::$cpt_post_id, |
| 128 | + ]; |
| 129 | + foreach ( $map as $key => $post_id ) { |
| 130 | + $request = new \WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $key, $post_id ) ); |
| 131 | + $request->set_body_params( $data ); |
| 132 | + |
| 133 | + $response = rest_get_server()->dispatch( $request ); |
| 134 | + $this->assertSame( 200, $response->get_status() ); |
| 135 | + |
| 136 | + $meta = get_post_meta( $post_id, 'material-hide-sections', false ); |
| 137 | + $this->assertNotEmpty( $meta ); |
| 138 | + $this->assertCount( 1, $meta ); |
| 139 | + $this->assertNotEmpty( 'test_value', $meta[0]['title'] ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test filter_body_class. |
| 145 | + */ |
| 146 | + public function test_filter_body_class() { |
| 147 | + global $post, $wp_query; |
| 148 | + $post = self::$post_id; |
| 149 | + $wp_query->is_single = true; |
| 150 | + update_post_meta( self::$post_id, 'material-hide-sections', [ 'title' => 1 ] ); |
| 151 | + $class = BlockEditor\filter_body_class( [] ); |
| 152 | + $this->assertTrue( in_array( 'has-hide-title', $class, true ) ); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Grant write permission to meta. |
| 157 | + */ |
| 158 | + protected function grant_write_permission() { |
| 159 | + // Ensure we have write permission. |
| 160 | + $user = $this->factory()->user->create( |
| 161 | + [ |
| 162 | + 'role' => 'editor', |
| 163 | + ] |
| 164 | + ); |
| 165 | + wp_set_current_user( $user ); |
| 166 | + } |
| 167 | +} |
0 commit comments