PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/kirki/tests/test-kirki.php

https://gitlab.com/aristath/mdl
PHP | 320 lines | 285 code | 31 blank | 4 comment | 2 complexity | 1f42df910acc7f70d0ff47528b060f8b MD5 | raw file
  1. <?php
  2. class Test_Kirki extends WP_UnitTestCase {
  3. public function init_customizer() {
  4. global $wp_customize;
  5. if ( ! isset( $wp_customize ) ) {
  6. if ( ! class_exists( 'WP_Customize_Manager' ) ) {
  7. require_once( ABSPATH . '/wp-includes/class-wp-customize-manager.php' );
  8. }
  9. $wp_customize = new WP_Customize_Manager();
  10. }
  11. return $wp_customize;
  12. }
  13. public function add_config() {
  14. Kirki::add_config( 'my_config_theme_mods', array(
  15. 'option_type' => 'theme_mod',
  16. 'capability' => 'edit_posts',
  17. ) );
  18. Kirki::add_config( 'my_config_options', array(
  19. 'option_type' => 'option',
  20. 'capability' => 'edit_posts',
  21. ) );
  22. Kirki::add_config( 'my_config_options_serialized', array(
  23. 'option_type' => 'option',
  24. 'option_name' => 'my_option',
  25. 'capability' => 'edit_posts',
  26. ) );
  27. }
  28. public function add_panel() {
  29. Kirki::add_panel( 'panel_id', array(
  30. 'priority' => 10,
  31. 'title' => 'My Title',
  32. 'description' => 'My Description',
  33. ) );
  34. }
  35. public function add_section() {
  36. Kirki::add_section( 'section_id', array(
  37. 'title' => 'My Title',
  38. 'description' => 'My Description',
  39. 'panel' => 'panel_id',
  40. 'priority' => 160,
  41. 'capability' => 'edit_theme_options',
  42. ) );
  43. }
  44. public function add_field() {
  45. Kirki::add_field( 'my_config_theme_mods', array(
  46. 'settings' => 'my_setting_theme_mods',
  47. 'label' => __( 'My custom control', 'translation_domain' ),
  48. 'section' => 'my_section',
  49. 'type' => 'text',
  50. 'priority' => 10,
  51. 'default' => 'some-default-value',
  52. ) );
  53. Kirki::add_field( 'my_config_options', array(
  54. 'settings' => 'my_setting_options',
  55. 'label' => __( 'My custom control', 'translation_domain' ),
  56. 'section' => 'my_section',
  57. 'type' => 'text',
  58. 'priority' => 10,
  59. 'default' => 'some-default-value',
  60. ) );
  61. Kirki::add_field( 'my_config_options_serialized', array(
  62. 'settings' => 'my_setting_options_serialized',
  63. 'label' => __( 'My custom control', 'translation_domain' ),
  64. 'section' => 'my_section',
  65. 'type' => 'text',
  66. 'priority' => 10,
  67. 'default' => 'some-default-value',
  68. ) );
  69. }
  70. public function add_background_fields() {
  71. Kirki::add_field( 'my_config_theme_mods', array(
  72. 'settings' => 'my_settings_test_background_theme_mod',
  73. 'section' => 'my_section',
  74. 'type' => 'background',
  75. 'default' => array(
  76. 'color' => '#333333',
  77. 'image' => 'http://foo.com/bar.png',
  78. 'repeat' => 'no-repeat',
  79. 'size' => 'cover',
  80. 'attach' => 'scroll',
  81. 'position' => 'center-bottom',
  82. 'opacity' => '.6',
  83. ),
  84. ) );
  85. Kirki::add_field( 'my_config_options', array(
  86. 'settings' => 'my_settings_test_background_options',
  87. 'section' => 'my_section',
  88. 'type' => 'background',
  89. 'default' => array(
  90. 'color' => '#333333',
  91. 'image' => 'http://foo.com/bar.png',
  92. 'repeat' => 'no-repeat',
  93. 'size' => 'cover',
  94. 'attach' => 'scroll',
  95. 'position' => 'center-bottom',
  96. 'opacity' => '.6',
  97. ),
  98. ) );
  99. Kirki::add_field( 'my_config_options_serialized', array(
  100. 'settings' => 'my_settings_test_background_options_serialized',
  101. 'section' => 'my_section',
  102. 'type' => 'background',
  103. 'default' => array(
  104. 'color' => '#333333',
  105. 'image' => 'http://foo.com/bar.png',
  106. 'repeat' => 'no-repeat',
  107. 'size' => 'cover',
  108. 'attach' => 'scroll',
  109. 'position' => 'center-bottom',
  110. 'opacity' => '.6',
  111. ),
  112. ) );
  113. }
  114. function add_controls_via_filter( $fields ) {
  115. // Add the controls
  116. $fields[] = array(
  117. 'label' => __( 'My custom control', 'translation_domain' ),
  118. 'section' => 'my_section',
  119. 'settings' => 'my_setting_3',
  120. 'type' => 'text',
  121. 'priority' => 10,
  122. 'option_type' => 'theme_mod',
  123. 'capability' => 'edit_posts',
  124. 'default' => 'some-default-value',
  125. );
  126. $fields[] = array(
  127. 'label' => __( 'My custom control 2', 'translation_domain' ),
  128. 'section' => 'my_section',
  129. 'settings' => 'my_setting_4',
  130. 'type' => 'checkbox',
  131. 'priority' => 20,
  132. 'option_type' => 'theme_mod',
  133. 'capability' => 'edit_theme_options',
  134. 'default' => '0',
  135. );
  136. return $fields;
  137. }
  138. public function test_config() {
  139. $this->add_config();
  140. // Default config
  141. $this->assertArrayHasKey( 'global', Kirki::$config );
  142. $this->assertEquals( 'edit_theme_options', Kirki::$config['global']['capability'] );
  143. $this->assertEquals( 'theme_mod', Kirki::$config['global']['option_type'] );
  144. // Custom config
  145. $this->assertArrayHasKey( 'my_config_theme_mods', Kirki::$config );
  146. $this->assertArrayHasKey( 'my_config_options', Kirki::$config );
  147. $this->assertArrayHasKey( 'my_config_options_serialized', Kirki::$config );
  148. $this->assertEquals( 'edit_posts', Kirki::$config['my_config_theme_mods']['capability'] );
  149. $this->assertEquals( 'option', Kirki::$config['my_config_options']['option_type'] );
  150. $this->assertEquals( 'my_option', Kirki::$config['my_config_options_serialized']['option_name'] );
  151. }
  152. public function test_panels() {
  153. $this->add_panel();
  154. $this->assertArrayHasKey( 'panel_id', Kirki::$panels );
  155. $this->assertEquals( 'panel_id', Kirki::$panels['panel_id']['id'] );
  156. $this->assertEquals( 10, Kirki::$panels['panel_id']['priority'] );
  157. $this->assertEquals( 'My Title', Kirki::$panels['panel_id']['title'] );
  158. $this->assertEquals( 'My Description', Kirki::$panels['panel_id']['description'] );
  159. }
  160. public function test_sections() {
  161. $this->add_section();
  162. $this->assertArrayHasKey( 'section_id', Kirki::$sections );
  163. $this->assertEquals( 'section_id', Kirki::$sections['section_id']['id'] );
  164. $this->assertEquals( 160, Kirki::$sections['section_id']['priority'] );
  165. $this->assertEquals( 'My Title', Kirki::$sections['section_id']['title'] );
  166. $this->assertEquals( 'My Description', Kirki::$sections['section_id']['description'] );
  167. }
  168. public function test_fields() {
  169. Kirki::$fields = array();
  170. $this->add_config();
  171. $this->add_field();
  172. $this->assertArrayHasKey( 'my_setting_theme_mods', Kirki::$fields );
  173. $this->assertEquals( 'My custom control', Kirki::$fields['my_setting_theme_mods']['label'] );
  174. $this->assertEquals( 'my_section', Kirki::$fields['my_setting_theme_mods']['section'] );
  175. $this->assertEquals( 'text', Kirki::$fields['my_setting_theme_mods']['type'] );
  176. $this->assertEquals( 10, Kirki::$fields['my_setting_theme_mods']['priority'] );
  177. $this->assertEquals( 'some-default-value', Kirki::$fields['my_setting_theme_mods']['default'] );
  178. $this->assertEquals( 'edit_posts', Kirki::$fields['my_setting_theme_mods']['capability'] );
  179. $this->assertEquals( 'theme_mod', Kirki::$fields['my_setting_theme_mods']['option_type'] );
  180. $this->assertEquals( 'option', Kirki::$fields['my_setting_options']['option_type'] );
  181. $this->assertEquals( 'option', Kirki::$fields['my_option[my_setting_options_serialized]']['option_type'] );
  182. }
  183. public function test_fields_via_filter() {
  184. Kirki::$fields = array();
  185. add_filter( 'kirki/fields', array( $this, 'add_controls_via_filter' ) );
  186. do_action( 'wp_loaded' );
  187. // my_setting
  188. $this->assertArrayHasKey( 'my_setting_3', Kirki::$fields );
  189. $this->assertArrayHasKey( 'my_setting_4', Kirki::$fields );
  190. $this->assertEquals( 'My custom control', Kirki::$fields['my_setting_3']['label'] );
  191. $this->assertEquals( 'my_section', Kirki::$fields['my_setting_3']['section'] );
  192. $this->assertEquals( 'text', Kirki::$fields['my_setting_3']['type'] );
  193. $this->assertEquals( 10, Kirki::$fields['my_setting_3']['priority'] );
  194. $this->assertEquals( 'some-default-value', Kirki::$fields['my_setting_3']['default'] );
  195. $this->assertEquals( 'edit_posts', Kirki::$fields['my_setting_3']['capability'] );
  196. }
  197. public function test_get_option() {
  198. Kirki::$config = null;
  199. Kirki::$fields = null;
  200. $this->add_config();
  201. $this->add_field();
  202. $this->assertEquals( 'some-default-value', Kirki::get_option( 'my_config_theme_mods', 'my_setting_theme_mods' ) );
  203. $this->assertEquals( 'some-default-value', Kirki::get_option( 'my_config_options', 'my_setting_options' ) );
  204. $this->assertEquals( 'some-default-value', Kirki::get_option( 'my_config_options_serialized', 'my_option[my_setting_options_serialized]' ) );
  205. Kirki::$config = null;
  206. Kirki::$fields = null;
  207. $this->add_config();
  208. $this->add_background_fields();
  209. $this->assertEquals(
  210. array(
  211. 'color' => '#333333',
  212. 'image' => 'http://foo.com/bar.png',
  213. 'repeat' => 'no-repeat',
  214. 'size' => 'cover',
  215. 'attach' => 'scroll',
  216. 'position' => 'center-bottom',
  217. 'opacity' => '.6',
  218. ),
  219. Kirki::get_option( 'my_config_theme_mods', 'my_settings_test_background_theme_mod' )
  220. );
  221. $this->assertEquals(
  222. array(
  223. 'color' => '#333333',
  224. 'image' => 'http://foo.com/bar.png',
  225. 'repeat' => 'no-repeat',
  226. 'size' => 'cover',
  227. 'attach' => 'scroll',
  228. 'position' => 'center-bottom',
  229. 'opacity' => '.6',
  230. ),
  231. Kirki::get_option( 'my_config_options', 'my_settings_test_background_options' )
  232. );
  233. $this->assertEquals(
  234. array(
  235. 'color' => '#333333',
  236. 'image' => 'http://foo.com/bar.png',
  237. 'repeat' => 'no-repeat',
  238. 'size' => 'cover',
  239. 'attach' => 'scroll',
  240. 'position' => 'center-bottom',
  241. 'opacity' => '.6',
  242. ),
  243. Kirki::get_option( 'my_config_options_serialized', 'my_option[my_settings_test_background_options_serialized]' )
  244. );
  245. Kirki::$config = null;
  246. Kirki::$fields = null;
  247. $this->add_config();
  248. $this->add_background_fields();
  249. set_theme_mod( 'my_settings_test_background_theme_mod_color', '#000000' );
  250. $this->assertEquals(
  251. array(
  252. 'color' => '#000000',
  253. 'image' => 'http://foo.com/bar.png',
  254. 'repeat' => 'no-repeat',
  255. 'size' => 'cover',
  256. 'attach' => 'scroll',
  257. 'position' => 'center-bottom',
  258. 'opacity' => '.6',
  259. ),
  260. Kirki::get_option( 'my_config_theme_mods', 'my_settings_test_background_theme_mod' )
  261. );
  262. update_option( 'my_settings_test_background_options_color', '#222222' );
  263. $this->assertEquals(
  264. array(
  265. 'color' => '#222222',
  266. 'image' => 'http://foo.com/bar.png',
  267. 'repeat' => 'no-repeat',
  268. 'size' => 'cover',
  269. 'attach' => 'scroll',
  270. 'position' => 'center-bottom',
  271. 'opacity' => '.6',
  272. ),
  273. Kirki::get_option( 'my_config_options', 'my_settings_test_background_options' )
  274. );
  275. update_option( 'my_option', array( 'my_settings_test_background_options_serialized_color' => '#444444' ) );
  276. $this->assertEquals(
  277. array(
  278. 'color' => '#444444',
  279. 'image' => 'http://foo.com/bar.png',
  280. 'repeat' => 'no-repeat',
  281. 'size' => 'cover',
  282. 'attach' => 'scroll',
  283. 'position' => 'center-bottom',
  284. 'opacity' => '.6',
  285. ),
  286. Kirki::get_option( 'my_config_options_serialized', 'my_option[my_settings_test_background_options_serialized]' )
  287. );
  288. }
  289. }