PageRenderTime 48ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/tosin/framework/CMB2/tests/test-cmb-field.php

https://gitlab.com/websumon/tosnib
PHP | 369 lines | 236 code | 84 blank | 49 comment | 0 complexity | 9328db8526071bc0a58de03a782bc94a MD5 | raw file
  1. <?php
  2. /**
  3. * CMB2_Field tests
  4. *
  5. * @package Tests_CMB2
  6. * @author WebDevStudios
  7. * @license GPL-2.0+
  8. * @link http://webdevstudios.com
  9. */
  10. require_once( 'cmb-tests-base.php' );
  11. class Test_CMB2_Field extends Test_CMB2 {
  12. /**
  13. * Set up the test fixture
  14. */
  15. public function setUp() {
  16. parent::setUp();
  17. $this->post_id = $this->factory->post->create();
  18. $this->field_args = array(
  19. 'name' => 'Name',
  20. 'id' => 'test_test',
  21. 'type' => 'text',
  22. 'attributes' => array(
  23. 'type' => 'number',
  24. 'disabled' => 'disabled',
  25. 'data-test' => 'data-value',
  26. 'data-test' => json_encode( array(
  27. 'name' => 'Name',
  28. 'id' => 'test_test',
  29. 'type' => 'text',
  30. ) ),
  31. ),
  32. 'before_field' => array( $this, 'before_field_cb' ),
  33. 'after_field' => 'after_field_static',
  34. 'row_classes' => array( $this, 'row_classes_array_cb' ),
  35. 'default' => array( $this, 'cb_to_set_default' ),
  36. 'render_row_cb' => array( $this, 'render_row_cb_test' ),
  37. 'label_cb' => array( $this, 'label_cb_test' ),
  38. );
  39. $this->object_id = $this->post_id;
  40. $this->object_type = 'post';
  41. $this->group = false;
  42. $this->field = new CMB2_Field( array(
  43. 'object_id' => $this->object_id,
  44. 'object_type' => $this->object_type,
  45. 'group' => $this->group,
  46. 'field_args' => $this->field_args,
  47. ) );
  48. }
  49. public function tearDown() {
  50. parent::tearDown();
  51. }
  52. public function test_cmb2_field_instance() {
  53. $this->assertInstanceOf( 'CMB2_Field', $this->field );
  54. }
  55. public function test_cmb2_before_and_after_field_callbacks() {
  56. ob_start();
  57. $this->field->peform_param_callback( 'before_field' );
  58. $this->field->peform_param_callback( 'after_field' );
  59. // grab the data from the output buffer and add it to our $content variable
  60. $content = ob_get_contents();
  61. ob_end_clean();
  62. $this->assertEquals( 'before_field_cb_test_testafter_field_static', $content );
  63. }
  64. public function test_cmb2_render_row_cb_field_callback() {
  65. ob_start();
  66. $this->field->render_field();
  67. $rendered = ob_get_clean();
  68. $this->assertEquals( 'test render cb', $rendered );
  69. }
  70. public function test_cmb2_label_cb_field_callback() {
  71. $label = $this->field->get_param_callback_result( 'label_cb' );
  72. $this->assertEquals( 'test label cb', $label );
  73. }
  74. public function test_cmb2_row_classes_field_callback_with_array() {
  75. // Add row classes dynamically with a callback that returns an array
  76. $classes = $this->field->row_classes();
  77. $this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout type name desc before after options_cb options attributes protocols default select_all_button multiple repeatable inline on_front show_names date_format time_format description preview_size render_row_cb label_cb id before_field after_field row_classes _id _name', $classes );
  78. }
  79. public function test_cmb2_default_field_callback_with_array() {
  80. // Add row classes dynamically with a callback that returns an array
  81. $default = $this->field->args( 'default' );
  82. $this->assertEquals( 'type, name, desc, before, after, options_cb, options, attributes, protocols, default, select_all_button, multiple, repeatable, inline, on_front, show_names, date_format, time_format, description, preview_size, render_row_cb, label_cb, id, before_field, after_field, row_classes, _id, _name', $default );
  83. }
  84. public function test_cmb2_row_classes_field_callback_with_string() {
  85. // Test with string
  86. $args = $this->field_args;
  87. // Add row classes dynamically with a callback that returns a string
  88. $args['row_classes'] = array( $this, 'row_classes_string_cb' );
  89. $field = new CMB2_Field( array(
  90. 'object_id' => $this->object_id,
  91. 'object_type' => $this->object_type,
  92. 'group' => $this->group,
  93. 'field_args' => $args,
  94. ) );
  95. $classes = $field->row_classes();
  96. $this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout callback with string', $classes );
  97. }
  98. public function test_cmb2_row_classes_string() {
  99. // Test with string
  100. $args = $this->field_args;
  101. // Add row classes statically as a string
  102. $args['row_classes'] = 'these are some classes';
  103. $field = new CMB2_Field( array(
  104. 'object_id' => $this->object_id,
  105. 'object_type' => $this->object_type,
  106. 'group' => $this->group,
  107. 'field_args' => $args,
  108. ) );
  109. $classes = $field->row_classes();
  110. $this->assertEquals( 'cmb-type-text cmb2-id-test-test table-layout these are some classes', $classes );
  111. }
  112. public function test_cmb2_should_show() {
  113. // Test with string
  114. $args = $this->field_args;
  115. // Add row classes statically as a string
  116. $args['show_on_cb'] = '__return_false';
  117. $field = new CMB2_Field( array(
  118. 'object_id' => $this->object_id,
  119. 'object_type' => $this->object_type,
  120. 'group' => $this->group,
  121. 'field_args' => $args,
  122. ) );
  123. $this->assertFalse( $field->should_show() );
  124. $field->args['show_on_cb'] = '__return_true';
  125. $this->assertTrue( $field->should_show() );
  126. }
  127. public function test_empty_field_with_empty_object_id() {
  128. $field = new CMB2_Field( array(
  129. 'field_args' => $this->field_args,
  130. ) );
  131. // data should be empty since we have no object id
  132. $this->assertEmpty( $field->get_data() );
  133. // add some xss for good measure
  134. $dirty_val = 'test<html><stuff><script>xss</script><a href="http://xssattackexamples.com/">Click to Download</a>';
  135. $cleaned_val = sanitize_text_field( $dirty_val );
  136. // Make sure it sanitizes as expected
  137. $this->assertEquals( $cleaned_val, $field->sanitization_cb( $dirty_val ) );
  138. // Sanitize/store the field
  139. $this->assertTrue( $field->save_field( $dirty_val ) );
  140. // Retrieve saved value(s)
  141. $this->assertEquals( $cleaned_val, cmb2_options( 0 )->get( $field->id() ) );
  142. $this->assertEquals( array( 'test_test' => $cleaned_val ), cmb2_options( 0 )->get_options() );
  143. }
  144. public function test_show_option_none() {
  145. $args = array(
  146. 'name' => 'Test Radio inline',
  147. 'desc' => 'field description (optional)',
  148. 'id' => 'radio_inline',
  149. 'type' => 'radio_inline',
  150. 'options' => array(
  151. 'standard' => 'Option One',
  152. 'custom' => 'Option Two',
  153. 'none' => 'Option Three',
  154. ),
  155. );
  156. $field = new CMB2_Field( array(
  157. 'field_args' => $args,
  158. ) );
  159. $this->assertFalse( $field->args( 'show_option_none' ) );
  160. $this->assertHTMLstringsAreEqual(
  161. '<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="standard"/><label for="radio_inline1">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="custom"/><label for="radio_inline2">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="none"/><label for="radio_inline3">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
  162. $this->render_field( $field )
  163. );
  164. $args['show_option_none'] = true;
  165. $field = new CMB2_Field( array(
  166. 'field_args' => $args,
  167. ) );
  168. $this->assertEquals( __( 'None', 'cmb2' ), $field->args( 'show_option_none' ) );
  169. $this->assertHTMLstringsAreEqual(
  170. '<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="" checked="checked"/><label for="radio_inline1">None</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard"/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom"/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none"/><label for="radio_inline4">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
  171. $this->render_field( $field )
  172. );
  173. $args['show_option_none'] = 'No Value';
  174. $field = new CMB2_Field( array(
  175. 'field_args' => $args,
  176. ) );
  177. $this->assertEquals( 'No Value', $field->args( 'show_option_none' ) );
  178. $this->assertHTMLstringsAreEqual(
  179. '<div class="cmb-row cmb-type-radio-inline cmb2-id-radio-inline cmb-inline"><div class="cmb-th"><label for="radio_inline">Test Radio inline</label></div><div class="cmb-td"><ul class="cmb2-radio-list cmb2-list"><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline1" value="" checked="checked"/><label for="radio_inline1">No Value</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline2" value="standard"/><label for="radio_inline2">Option One</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline3" value="custom"/><label for="radio_inline3">Option Two</label></li><li><input type="radio" class="cmb2-option" name="radio_inline" id="radio_inline4" value="none"/><label for="radio_inline4">Option Three</label></li></ul><p class="cmb2-metabox-description">field description (optional)</p></div></div>',
  180. $this->render_field( $field )
  181. );
  182. }
  183. public function test_multiple_meta_rows() {
  184. $prefix = 'testing';
  185. $post_id = $this->post_id;
  186. $array_val = array( 'check1', 'check2' );
  187. $cmb_demo = cmb2_get_metabox( array(
  188. 'id' => $prefix . 'metabox',
  189. 'title' => __( 'Test Metabox', 'cmb2' ),
  190. 'object_types' => array( 'page', ), // Post type
  191. 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
  192. 'context' => 'normal',
  193. 'priority' => 'high',
  194. 'show_names' => true, // Show field names on the left
  195. // 'cmb_styles' => false, // false to disable the CMB stylesheet
  196. // 'closed' => true, // true to keep the metabox closed by default
  197. ), $post_id );
  198. $field_id = $cmb_demo->add_field( array(
  199. 'name' => __( 'Test Multi Checkbox', 'cmb2' ),
  200. 'desc' => __( 'field description (optional)', 'cmb2' ),
  201. 'id' => $prefix . 'multicheckbox',
  202. 'type' => 'multicheck',
  203. 'multiple' => true, // Store values in individual rows
  204. 'options' => array(
  205. 'check1' => __( 'Check One', 'cmb2' ),
  206. 'check2' => __( 'Check Two', 'cmb2' ),
  207. 'check3' => __( 'Check Three', 'cmb2' ),
  208. ),
  209. ) );
  210. $field = $cmb_demo->get_field( $field_id );
  211. $saved = $field->save_field( $array_val );
  212. $this->assertEquals( 2, $saved );
  213. $this->assertEquals( $array_val, $field->get_data() );
  214. $this->assertEquals( $array_val, get_post_meta( $post_id, $field_id ) );
  215. $val = get_post_meta( $post_id, $field_id, 1 );
  216. $this->assertEquals( reset( $array_val ), $val );
  217. }
  218. public function test_set_get_filters() {
  219. // Value to set
  220. $array_val = array( 'check1', 'check2' );
  221. // Set the field value normally
  222. $this->field->save_field( $array_val );
  223. // Verify that field was saved succesfully to post-meta
  224. $this->assertEquals( $array_val, get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
  225. // Now delete the post-meta
  226. delete_post_meta( $this->field->object_id, $this->field->_id() );
  227. // Verify that the post-meta no longer exists
  228. $this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
  229. // Now filter the setting of the meta.. will use update_option instead
  230. add_filter( "cmb2_override_{$this->field->_id()}_meta_save", array( $this, 'override_set' ), 10, 4 );
  231. // Set the value
  232. $this->field->save_field( $array_val );
  233. // Verify that the post-meta is still empty
  234. $this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );
  235. // Re-create the option key that we used to save the data
  236. $opt_key = 'test-'. $this->field->object_id . '-' . $this->field->_id();
  237. // And retrieve the option
  238. $opt = get_option( $opt_key );
  239. // Verify it is the value we set
  240. $this->assertEquals( $array_val, $opt );
  241. // Now retireve the value.. will default to getting from post-meta
  242. $value = $this->field->get_data();
  243. // Verify that there's still nothing there in post-meta
  244. $this->assertFalse( !! $value );
  245. // Now filter the getting of the meta, which will use get_option
  246. add_filter( "cmb2_override_{$this->field->_id()}_meta_value", array( $this, 'override_get' ), 10, 4 );
  247. // Get the value
  248. $value = $this->field->get_data();
  249. // Verify that the data we retrieved now matches the value we set
  250. $this->assertEquals( $array_val, $value );
  251. }
  252. public function before_field_cb( $args ) {
  253. echo 'before_field_cb_' . $args['id'];
  254. }
  255. public function row_classes_array_cb( $args ) {
  256. /**
  257. * Side benefit: this will call out when default args change
  258. */
  259. return array_keys( $args );
  260. }
  261. public function row_classes_string_cb( $args ) {
  262. return 'callback with string';
  263. }
  264. public function render_row_cb_test( $field_args ) {
  265. echo 'test render cb';
  266. }
  267. public function label_cb_test( $field_args ) {
  268. echo 'test label cb';
  269. }
  270. public function cb_to_set_default( $args ) {
  271. /**
  272. * Side benefit: this will call out when default args change
  273. */
  274. return implode( ', ', array_keys( $args ) );
  275. }
  276. public function override_set( $override, $args, $field_args, $field ) {
  277. $opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
  278. $updated = update_option( $opt_key, $args['value'] );
  279. return true;
  280. }
  281. public function override_get( $override_val, $object_id, $args, $field ) {
  282. $opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
  283. return get_option( $opt_key );
  284. }
  285. }