-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathFrmUnitTest.php
More file actions
846 lines (710 loc) · 25.1 KB
/
Copy pathFrmUnitTest.php
File metadata and controls
846 lines (710 loc) · 25.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<?php
class FrmUnitTest extends WP_UnitTestCase {
/**
* Track if an install has happened to avoid installing too often.
*
* @var bool
*/
protected static $installed = false;
protected $user_id = 0;
protected $contact_form_key = 'contact-with-email';
protected $contact_form_field_count = 10;
protected $all_fields_form_key = 'all_field_types';
protected $all_field_types_count = 50;
protected $repeat_sec_form_key = 'rep_sec_form';
protected $create_post_form_key = 'create-a-post';
protected $is_pro_active = false;
/**
* Narrows the inherited property to the Formidable factory so static analysis
* can resolve $this->factory->form, ->field and ->entry.
*
* @var FrmUnitTestFactory
*/
protected $factory;
/**
* @var FrmUnitTest
*/
protected static $instance;
public static function wpSetUpBeforeClass() {
$_POST = array();
}
public static function wpTearDownAfterClass() {
}
public function setUp(): void {
self::$instance = $this;
parent::setUp();
self::reset_shared_state();
// The JavaScript antispam check doesn't work with unit tests so turn it off.
add_filter( 'frm_run_antispam', '__return_false' );
$this->is_pro_active = get_option( 'frmpro-authorized' );
if ( is_multisite() && ! $this->is_pro_active ) {
// WP unit testing bootstrap doesn't bother hooking into `pre_site_option` so we need to get_option() instead.
$this->is_pro_active = get_site_option( 'frmpro-authorized' );
}
FrmHooksController::trigger_load_hook( 'load_admin_hooks' );
$this->factory = new FrmUnitTestFactory();
$this->factory->form = new Form_Factory( $this );
$this->factory->field = new Field_Factory( $this );
$this->factory->entry = new Entry_Factory( $this );
}
/**
* Clear state that outlives a single test.
*
* PHPUnit gives each test its own database transaction, but globals and singletons live
* for the whole process, so one test can leave another reading values it never set. A
* test that needs any of this state should set it up itself; anything that passes only
* because a previous test set it up is relying on the suite's ordering.
*
* @return void
*/
public static function reset_shared_state() {
// Read by FrmXMLHelper::populate_postmeta(), which takes a different code path when set.
$GLOBALS['frm_duplicate_ids'] = array();
// Populated in production only when a form renders, via FrmHoneypot::maybe_render_field().
foreach ( array( 'FrmFormState', 'FrmProFormState' ) as $state_class ) {
if ( ! class_exists( $state_class ) ) {
continue;
}
$instance = new ReflectionProperty( $state_class, 'instance' );
$instance->setAccessible( true );
$instance->setValue( null, null );
}
}
/**
* Some of the tests for FrmDb are triggering a transaction commit, preventing further tests from working.
* This is a temporary workaround until we review FrmDb tests in detail.
*/
public static function empty_tables() {
global $wpdb;
foreach ( self::get_table_names() as $table ) {
$exists = $wpdb->get_var( 'DESCRIBE ' . $table );
if ( $exists ) {
$wpdb->query( "TRUNCATE $table" );
}
}
}
/**
* @covers FrmAppController::install()
*/
public static function frm_install() {
if ( ! defined( 'WP_IMPORTING' ) ) {
// Set this to false so all our tests won't be done with this active
define( 'WP_IMPORTING', false );
}
if ( self::$installed ) {
/**
* Empty the tables before re-importing.
*
* Importing over forms that already exist updates those forms but skips their
* entries, so a re-import on its own leaves the fixture set with forms and no
* entries. Every class that runs later in the same process then inherits that,
* and which classes those are depends on how the suite happens to be ordered.
* Truncating first makes the re-import restore entries too, so a class gets the
* same fixture data no matter what ran before it.
*/
self::empty_tables();
self::import_xml();
return;
}
$allow_xml_mime_types_function = function ( $mimes ) {
$mimes['xml'] = 'application/xml';
return $mimes;
};
// Allow XML files in import as we're importing several XML files below.
add_filter( 'mime_types', $allow_xml_mime_types_function );
if ( is_multisite() ) {
// Mimes get changed because of add_filter( 'upload_mimes', 'check_upload_mimes' ); in ms-default-filters.php (A WordPress file).
add_filter( 'upload_mimes', $allow_xml_mime_types_function, 11 );
}
/**
* This is required to run on newer versions of WP without triggering an error:
* file_get_contents(/tmp/wordpress/src/wp-includes/js/wp-emoji-loader.min.js): failed to open stream: No such file or directory
* Our tests do not require the emoji scripts so we can just disable them.
*/
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
self::isolate_install_from_plugin_folder();
FrmHooksController::trigger_load_hook( 'load_admin_hooks' );
FrmAppController::install();
self::do_tables_exist();
/**
* Start from empty tables even on the first install of the process.
*
* WordPress' install.php drops and recreates the tables it knows about on every process,
* but Formidable's tables are not among them, so rows from a previous run survive. The
* import below would then update the existing forms and skip their entries, leaving a
* fixture set with forms and no entries. That is what makes a re-used database produce
* failures unrelated to the code under test, and what breaks a parallel run as soon as
* one worker runs a second process.
*/
self::empty_tables();
self::import_xml();
self::create_files();
self::$installed = true;
}
/**
* Keep the install from writing into the plugin folder or reaching for the network.
*
* FrmAppHelper::plugin_path() and plugin_url() are derived from __DIR__, which PHP
* resolves through symlinks, so they point at the real plugin folder rather than the
* copy inside the wordpress-develop checkout the tests run against. Everything the
* install writes relative to those therefore lands in the working tree of whoever is
* running the suite, and is picked up by the site they develop against. Two writes do
* that, and both are redirected here rather than skipped, so the code under test still
* runs the same path it runs in production.
*
* @since 6.35
*
* @return void
*/
private static function isolate_install_from_plugin_folder() {
// Generate the stylesheet under uploads, which is disposable, instead of over css/formidableforms.css.
add_filter( 'frm_add_css_to_uploads_dir', '__return_true' );
// Answer the request FrmMigrate makes before deciding to delete the plugin's .htaccess.
add_filter( 'pre_http_request', 'FrmUnitTest::respond_to_plugin_asset_request', 10, 3 );
}
/**
* Serve requests for the plugin's own assets from here instead of over HTTP.
*
* A test run has no web server able to serve the plugin, so a request for one of its
* files can only fail. FrmMigrate::maybe_delete_htaccess_file() reads that failure as a
* server that cannot be trusted with the file and deletes the plugin's .htaccess, which
* is tracked in the repository. Requests to anywhere else are left alone.
*
* @since 6.35
*
* @param array|false|WP_Error $response A preemptive response, or false to let the request run.
* @param array $args Request arguments.
* @param string $url The request URL.
*
* @return array|false|WP_Error
*/
public static function respond_to_plugin_asset_request( $response, $args, $url ) {
if ( ! str_starts_with( $url, FrmAppHelper::plugin_url() . '/' ) ) {
return $response;
}
return array(
'headers' => array(),
'body' => '',
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => null,
);
}
public static function get_table_names() {
global $wpdb;
$tables = array(
$wpdb->prefix . 'frm_fields',
$wpdb->prefix . 'frm_forms',
$wpdb->prefix . 'frm_items',
$wpdb->prefix . 'frm_item_metas',
);
if ( is_multisite() && is_callable( 'FrmProCopy::table_name' ) ) {
$tables[] = FrmProCopy::table_name();
}
return $tables;
}
public static function do_tables_exist( $should_exist = true ) {
global $wpdb;
$method = $should_exist ? 'assertNotEmpty' : 'assertEmpty';
foreach ( self::get_table_names() as $table_name ) {
$message = $table_name . ' table failed to ' . ( $should_exist ? 'install' : 'uninstall' );
self::$method( $wpdb->query( 'DESCRIBE ' . $table_name ), $message );
}
}
public static function import_xml() {
// Install test data in older format
add_filter( 'frm_default_templates_files', 'FrmUnitTest::install_data' );
FrmXMLController::add_default_templates();
$form = FrmForm::getOne( 'contact-db12' );
self::assertSame( 'contact-db12', $form->form_key );
}
public static function create_files() {
if ( ! is_callable( 'FrmProFileImport::import_attachment' ) ) {
return;
}
add_filter( 'frm_should_import_files', '__return_true' );
$single_file_upload_field = FrmField::getOne( 'single-file-upload-field' );
$multi_file_upload_field = FrmField::getOne( 'multi-file-upload-field' );
$file_urls = array(
array(
'val' => 'https://s3.amazonaws.com/fp.strategy11.com/images/knowledgebase/global-settings_enter-license1.png',
'field' => $single_file_upload_field,
'entry' => 'jamie_entry_key',
),
array(
'val' => 'https://formidableforms.com/wp-content/uploads/formidable/formidablepro.real_estate_listings.2015-08-10.xml',
'field' => $single_file_upload_field,
'entry' => 'steph_entry_key',
),
array(
'val' => array(
'https://s3.amazonaws.com/fp.strategy11.com/images/knowledgebase/global-settings_enter-license1.png',
'https://s3.amazonaws.com/fp.strategy11.com/images/knowledgebase/create-a-form_add-new.png',
'https://formidableforms.com/wp-content/uploads/formidable/formidablepro.real_estate_listings.2015-08-10.xml',
),
'field' => $multi_file_upload_field,
'entry' => 'jamie_entry_key',
),
array(
'val' => 'https://formidableforms.com/wp-content/uploads/formidable/formidablepro.real_estate_listings.2015-08-10.xml',
'field' => FrmField::getOne( 'file_upload_single' ),
'entry' => 'many_files_key',
),
array(
'val' => array(
'https://cdn.formidableforms.com/wp-content/uploads/2016/11/goal-form.png',
'https://cdn.formidableforms.com/wp-content/uploads/2016/11/goal-progress.png',
'https://cdn.formidableforms.com/wp-content/uploads/2016/09/new-graph-types1.png',
),
'field' => FrmField::getOne( 'file_upload_multiple' ),
'entry' => 'many_files_key',
),
array(
'val' => array(
'https://cdn.formidableforms.com/wp-content/uploads/2017/07/user-registration-multisite.jpeg',
'https://cdn.formidableforms.com/wp-content/uploads/2017/07/lost-password-form.png',
'https://cdn.formidableforms.com/wp-content/uploads/2017/07/login-form.png',
),
'field' => FrmField::getOne( 'file_upload_multiple_repeating' ),
'entry' => 'file-repeat-child-one',
),
array(
'val' => array(
'https://cdn.formidableforms.com/wp-content/uploads/2016/11/normal-section-job-history-1.png',
'https://cdn.formidableforms.com/wp-content/uploads/2016/11/repeating-section-job-history-1.png',
),
'field' => FrmField::getOne( 'file_upload_multiple_repeating' ),
'entry' => 'file-repeat-child-two',
),
);
$uploads_dir = wp_upload_dir()['basedir'] . '/formidable/';
$test = new FrmUnitTest();
foreach ( $file_urls as $values ) {
$vals = (array) $values['val'];
$media_ids = false;
foreach ( $vals as $val ) {
$filename = basename( $val );
$path = $uploads_dir . $filename;
if ( ! file_exists( $path ) && is_object( $values['field'] ) ) {
// File may be in formidable folder or it may be in the form_id folder so check the form as well.
$form_id_path = $uploads_dir . $values['field']->form_id . '/' . $filename;
if ( file_exists( $form_id_path ) ) {
copy( $form_id_path, $path );
}
unset( $form_id_path );
}
if ( ! file_exists( $path ) ) {
continue;
}
if ( ! is_array( $media_ids ) ) {
$media_ids = array();
}
$media_ids[] = $test->run_private_method( array( 'FrmProFileImport', 'attach_existing_image' ), array( $filename ) );
}
if ( is_array( $media_ids ) ) {
$media_ids = implode( ',', $media_ids );
}
if ( false === $media_ids ) {
$media_ids = FrmProFileImport::import_attachment( $values['val'], $values['field'] );
}
if ( is_array( $values['val'] ) ) {
$media_ids = explode( ',', $media_ids );
} else {
$is_file_val = is_numeric( $media_ids ) || strpos( $media_ids, ',' );
self::assertTrue( $is_file_val, 'The following file is not importing correctly: ' . $values['val'] );
}
// Insert into entries
$entry_id = FrmEntry::get_id_by_key( $values['entry'] );
FrmEntryMeta::add_entry_meta( $entry_id, $values['field']->id, null, $media_ids );
}
}
public function get_all_fields_for_form_key( $form_key ) {
$field_totals = array(
$this->all_fields_form_key => $this->is_pro_active ? $this->all_field_types_count : $this->all_field_types_count - 3,
$this->create_post_form_key => 10,
$this->contact_form_key => $this->contact_form_field_count,
$this->repeat_sec_form_key => 3,
);
$expected_field_num = $field_totals[ $form_key ] ?? 0;
$form_id = $this->factory->form->get_id_by_key( $form_key );
$fields = FrmField::get_all_for_form( $form_id, '', 'include' );
$actual_field_num = count( $fields );
$this->assertSame( $expected_field_num, $actual_field_num, $actual_field_num . ' fields were retrieved for ' . $form_key . ' form, but ' . $expected_field_num . ' were expected. This could mean that certain fields were not imported correctly.' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
return $fields;
}
/**
* Set the current user to 1
*/
public function set_current_user_to_1() {
$this->set_user_by_role( 'administrator' );
}
public function set_current_user_to_username( $login ) {
$user = get_user_by( 'login', $login );
if ( $user ) {
wp_set_current_user( $user->ID );
}
}
/**
* Get a user by the specified role and set them as the current user
*
* @param string $role
*
* @return WP_User
*/
public function set_user_by_role( $role ) {
$user = $this->get_user_by_role( $role );
wp_set_current_user( $user->ID );
$this->assertTrue( current_user_can( $role ), 'Failed setting the current user role' );
FrmAppHelper::maybe_add_permissions();
return $user->ID;
}
/**
* Get a user of a specific role
*
* @param string $role
*
* @return WP_User
*/
public function get_user_by_role( $role ) {
$users = get_users(
array(
'role' => $role,
'number' => 1,
)
);
if ( ! $users ) {
$this->fail( 'No users with this role currently exist.' );
return null;
}
return reset( $users );
}
public function go_to_new_post() {
$new_post = $this->factory->post->create_and_get();
$page = get_permalink( $new_post->ID );
$this->set_front_end( $page );
return $new_post->ID;
}
public function set_front_end( $page = '' ) {
if ( $page === '' ) {
$page = home_url( '/' );
}
$this->clean_up_global_scope();
$this->go_to( $page );
$this->assertFalse( is_admin(), 'Failed to switch to the front-end' );
}
public function set_admin_screen( $page = 'index.php' ) {
global $current_screen;
$screens = array(
'index.php' => array(
'base' => 'dashboard',
'id' => 'dashboard',
),
'admin.php?page=formidable' => array(
'base' => 'admin',
'id' => 'toplevel_page_formidable',
),
);
if ( $page === 'formidable-edit' ) {
$form = $this->factory->form->get_object_by_id( $this->contact_form_key );
$page = 'admin.php?page=formidable&frm_action=edit&id=' . $form->id;
$screens[ $page ] = $screens['admin.php?page=formidable'];
} elseif ( ! isset( $screens[ $page ] ) ) {
$base = explode( '.php', $page );
$screens[ $page ] = array( 'base' => reset( $base ) );
}
$_GET = array();
$_POST = array();
$_REQUEST = array();
$GLOBALS['taxnow'] = '';
$GLOBALS['typenow'] = '';
$screen = (object) $screens[ $page ];
$hook = parse_url( $page );
$GLOBALS['hook_suffix'] = $hook['path'];
set_current_screen();
$this->set_get_params( $page );
$this->assertTrue( $current_screen->in_admin(), 'Failed to switch to the back-end' );
$this->assertTrue( is_admin(), 'Failed to switch to the back-end' );
$this->assertSame( $screen->base, $current_screen->base, $page );
FrmHooksController::trigger_load_hook();
}
/**
* Set the admin page parameters for the later code to use
*
* @since 3.0
*
* @param string $url
*
* @return void
*/
protected function set_get_params( $url ) {
if ( ! str_contains( $url, '?' ) ) {
return;
}
list( $base, $url_params ) = explode( '?', $url );
global $pagenow;
$pagenow = $base;
$_GET['pagenow'] = $base;
$_POST['pagenow'] = $base;
if ( empty( $url_params ) ) {
return;
}
$url_params = explode( '&', $url_params );
foreach ( $url_params as $param ) {
list( $name, $value ) = explode( '=', $param );
$_GET[ $name ] = $value;
$_REQUEST[ $name ] = $value;
if ( $name !== 'post' ) {
continue;
}
global $post;
$post = $this->factory->post->get_object_by_id( $value );
}
}
public function clean_up_global_scope() {
parent::clean_up_global_scope();
if ( isset( $GLOBALS['current_screen'] ) ) {
unset( $GLOBALS['current_screen'] );
}
global $frm_vars;
$frm_vars = array(
'load_css' => false,
'forms_loaded' => array(),
'created_entries' => array(),
'pro_is_authorized' => false,
'next_page' => array(),
'prev_page' => array(),
);
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
if ( class_exists( 'FrmProEddController' ) ) {
$frmedd_update = new FrmProEddController();
$frm_vars['pro_is_authorized'] = $frmedd_update->pro_is_authorized();
}
}
public function get_footer_output() {
ob_start();
do_action( 'wp_footer' );
return ob_get_clean();
}
/**
* @return array
*/
public static function install_data() {
return array(
__DIR__ . '/testdata.xml',
__DIR__ . '/free-form.xml',
__DIR__ . '/editform.xml',
__DIR__ . '/file-upload.xml',
);
}
/**
* @param array $type
*/
public static function generate_xml( $type, $xml_args ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
// Code copied from FrmXMLController::generate_xml
global $wpdb;
$type = (array) $type;
if ( in_array( 'items', $type, true ) && ! in_array( 'forms', $type, true ) ) {
// Make sure the form is included if there are entries
$type[] = 'forms';
}
if ( in_array( 'forms', $type, true ) ) {
// Include actions with forms
$type[] = 'actions';
}
$tables = array(
'items' => $wpdb->prefix . 'frm_items',
'forms' => $wpdb->prefix . 'frm_forms',
'posts' => $wpdb->posts,
'styles' => $wpdb->posts,
'actions' => $wpdb->posts,
);
$defaults = array( 'ids' => false );
$args = wp_parse_args( $xml_args, $defaults );
// Make sure ids are numeric.
if ( is_array( $args['ids'] ) && $args['ids'] ) {
$args['ids'] = array_filter( $args['ids'], 'is_numeric' );
}
$records = array();
foreach ( $type as $tb_type ) {
$where = array();
$join = '';
$table = $tables[ $tb_type ];
$select = $table . '.id';
$query_vars = array();
switch ( $tb_type ) {
case 'forms':
// Add forms.
if ( $args['ids'] ) {
$where[] = array(
'or' => 1,
$table . '.id' => $args['ids'],
$table . '.parent_form_id' => $args['ids'],
);
} else {
$where[ $table . '.status !' ] = 'draft';
}
break;
case 'actions':
$select = $table . '.ID';
$where['post_type'] = FrmFormActionsController::$action_post_type;
if ( ! empty( $args['ids'] ) ) {
$where['menu_order'] = $args['ids'];
}
break;
case 'items':
// $join = "INNER JOIN {$wpdb->prefix}frm_item_metas im ON ($table.id = im.item_id)";
if ( $args['ids'] ) {
$where[ $table . '.form_id' ] = $args['ids'];
}
break;
case 'styles':
// Loop through all exported forms and get their selected style IDs
$form_ids = $args['ids'];
$style_ids = array();
foreach ( $form_ids as $form_id ) {
$form_data = FrmForm::getOne( $form_id );
// For forms that have not been updated while running 2.0, check if custom_style is set
if ( isset( $form_data->options['custom_style'] ) ) {
$style_ids[] = $form_data->options['custom_style'];
}
unset( $form_id, $form_data );
}
$select = $table . '.ID';
$where['post_type'] = 'frm_styles';
// Only export selected styles
if ( $style_ids ) {
$where['ID'] = $style_ids;
}
break;
default:
$select = $table . '.ID';
$join = ' INNER JOIN ' . $wpdb->postmeta . ' pm ON (pm.post_id=' . $table . '.ID)';
$where['pm.meta_key'] = 'frm_form_id';
if ( empty( $args['ids'] ) ) {
$where['pm.meta_value >'] = 1;
} else {
$where['pm.meta_value'] = $args['ids'];
}
break;
}
$records[ $tb_type ] = FrmDb::get_col( $table . $join, $where, $select );
unset( $tb_type );
}
$xml_header = '<?xml version="1.0" encoding="' . esc_attr( get_bloginfo( 'charset' ) ) . "\" ?>\n";
ob_start();
include FrmAppHelper::plugin_path() . '/classes/views/xml/xml.php';
$xml_body = ob_get_clean();
$xml = $xml_header . $xml_body;
$cwd = getcwd();
$path = "{$cwd}/temp.xml";
@chmod( $path, 0755 );
$fw = fopen( $path, 'w' );
fwrite( $fw, $xml, strlen( $xml ) );
fclose( $fw );
return $path;
}
/**
* Create an administrator, editor, and subscriber
*
* @since 2.0
*
* @return void
*/
protected function create_users() {
$has_user = get_user_by( 'email', 'admin@mail.com' );
if ( $has_user ) {
return;
}
$admin_args = array(
'user_login' => 'admin',
'user_email' => 'admin@mail.com',
'user_pass' => 'admin',
'role' => 'administrator',
);
$admin = $this->factory->user->create_object( $admin_args );
$this->assertNotEmpty( $admin );
$editor_args = array(
'user_login' => 'editor',
'user_email' => 'editor@mail.com',
'user_pass' => 'editor',
'role' => 'editor',
);
$editor = $this->factory->user->create_object( $editor_args );
$this->assertNotEmpty( $editor );
$subscriber_args = array(
'user_login' => 'subscriber',
'user_email' => 'subscriber@mail.com',
'user_pass' => 'subscriber',
'role' => 'subscriber',
);
$subscriber = $this->factory->user->create_object( $subscriber_args );
$this->assertNotEmpty( $subscriber );
}
/**
* @param array $method
* @param array $args
*/
protected function run_private_method( $method, $args = array() ) {
$m = new ReflectionMethod( $method[0], $method[1] );
$m->setAccessible( true );
return $m->invokeArgs( is_string( $method[0] ) ? null : $method[0], $args );
}
/**
* Skip this if running < php 5.3
*
* @param mixed $object
* @param string $property
*
* @return ReflectionProperty
*/
protected function get_accessible_property( $object, $property ) {
$rc = new ReflectionClass( $object );
$p = $rc->getProperty( $property );
$p->setAccessible( true );
return $p;
}
protected function get_private_property( $object, $property ) {
$p = $this->get_accessible_property( $object, $property );
return $p->getValue( is_object( $object ) ? $object : null );
}
protected function set_private_property( $object, $property, $value ) {
$p = $this->get_accessible_property( $object, $property );
if ( ! is_object( $object ) && ! is_null( $object ) ) {
// Avoid passing a non-object, non-null value to setValue.
// Otherwise a ReflectionProperty::setValue() with a 1st argument which is not null or an object message will get logged.
$object = null;
}
$p->setValue( $object, $value );
}
protected function check_php_version( $required ) {
if ( version_compare( phpversion(), $required, '<' ) ) {
$this->markTestSkipped( 'Test requires PHP > ' . $required );
}
}
/**
* @param string $role
*/
protected function use_frm_role( $role ) {
switch ( $role ) {
case 'loggedout':
wp_set_current_user( null );
break;
case 'formidable_custom_role':
$user = wp_get_current_user();
// Remove any standard roles to make room for a custom one
foreach ( array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
$user->remove_role( $role );
}
add_role( 'formidable_custom_role', 'Custom Role' );
$user->add_role( 'formidable_custom_role' );
wp_set_current_user( $user->ID );
break;
default:
$this->set_user_by_role( $role );
break;
}
}
}