'
- );
- }
- }
- }
- }
- // Load editor & hide description
- hideDesc();
- } catch ( e ) {
- if ( debug ) {
- console.log( 'loadFieldEditor(): ' + e );
- }
- }
- }
- function parseShortcode( data ) {
- try {
- // Clean up fields by resetting them
- fbForm.fields = {};
- // Add new fields
- if ( ! data ) {
- fbForm.fields = defaultFields;
- } else {
- jQuery.each( data.fields, function ( index, value ) {
- if ( 1 === parseInt( value.required, 10 ) ) {
- value.required = 'true';
- }
- fbForm.fields[ index ] = value;
- } );
- fbForm.to = data.to;
- fbForm.subject = data.subject;
- }
- } catch ( e ) {
- if ( debug ) {
- console.log( 'parseShortcode(): ' + e );
- }
- }
- }
- function removeOption( optionId ) {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- var thisVal = jQuery( '#fb-option' + optionId ).val();
- var thisType = jQuery( '#fb-new-type' ).val();
- // Remove from right
- jQuery( '#fb-option-box-' + optionId ).remove();
- // Remove from preview
- if ( thisType === 'radio' ) {
- jQuery( '#fb-radio-' + thisId + '-' + optionId ).remove();
- } else if ( 'checkbox-multiple' === thisType ) {
- jQuery( '#fb-checkbox-multiple-' + thisId + '-' + optionId ).remove();
- } else {
- jQuery( '#fb-' + thisId + '-' + optionId ).remove();
- }
- // Remove from fbForm object
- var idx = fbForm.fields[ thisId ].options.indexOf( thisVal );
- if ( idx !== -1 ) {
- fbForm.fields[ thisId ].options.splice( idx, 1 );
- }
- } catch ( e ) {
- if ( debug ) {
- console.log( 'removeOption(): ' + e );
- }
- }
- }
- function removeOptions() {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- jQuery( '#fb-options' ).hide();
- if ( optionsCache[ thisId ] === undefined ) {
- optionsCache[ thisId ] = {};
- }
- optionsCache[ thisId ].options = fbForm.fields[ thisId ].options; // Save options in case they change their mind
- fbForm.fields[ thisId ].options = []; // Removes all options
- } catch ( e ) {
- if ( debug ) {
- console.log( 'removeOptions(): ' + e );
- }
- }
- }
- function sendShortcodeToEditor() {
- try {
- // Serialize fields
- jQuery( 'div#sortable div.fb-new-fields' ).each( function ( index ) {
- var thisId = jQuery( this ).attr( 'fieldid' );
- fbForm.fields[ thisId ].order = index;
- } );
- // Export to WYSIWYG editor
- jQuery.post( ajaxurl, fbForm, function ( response ) {
- var isVisual = jQuery( '#edButtonPreview', window.parent.document ).hasClass( 'active' );
- /* WP 3.3+ */
- if ( ! isVisual ) {
- isVisual = jQuery( '#wp-content-wrap', window.parent.document ).hasClass( 'tmce-active' );
- }
-
- var win = window.dialogArguments || opener || parent || top;
- var currentCode;
- if ( isVisual ) {
- currentCode = win.tinyMCE.activeEditor.getContent();
- } else {
- currentCode = jQuery( '#editorcontainer textarea', window.parent.document ).val();
- /* WP 3.3+ */
- if ( typeof currentCode !== 'string' ) {
- currentCode = jQuery( '.wp-editor-area', window.parent.document ).val();
- }
- }
- var regexp = new RegExp(
- '\\[contact-form\\b.*?\\/?\\](?:[\\s\\S]+?\\[\\/contact-form\\])?'
- );
-
- // Remove new lines that cause BR tags to show up
- response = response.replace( /\n/g, ' ' );
- // Convert characters to comma
- response = response.replace( /%26#x002c;/g, ',' );
-
- // Add new shortcode
- if ( currentCode.match( regexp ) ) {
- if ( isVisual ) {
- win.tinyMCE.activeEditor.execCommand(
- 'mceSetContent',
- false,
- currentCode.replace( regexp, response )
- );
- } else {
- // looks like the visual editor is disabled,
- // update the contents of the post directly
- jQuery( '#content', window.parent.document ).val(
- currentCode.replace( regexp, response )
- );
- }
- } else {
- try {
- win.send_to_editor( response );
- } catch ( e ) {
- if ( isVisual ) {
- win.tinyMCE.activeEditor.execCommand( 'mceInsertContent', false, response );
- } else {
- // looks like the visual editor is disabled,
- // update the contents of the post directly
- jQuery( '#content', window.parent.document ).val( currentCode + response );
- }
- }
- }
- hidePopup();
- } );
- } catch ( e ) {
- if ( debug ) {
- console.log( 'sendShortcodeToEditor(): ' + e );
- }
- }
- }
- function showDesc() {
- jQuery( '#fb-desc' ).show();
- jQuery( '#fb-add-field' ).hide();
- }
- function showAndHideMessage( message ) {
- try {
- var newMessage = ! message ? GrunionFB_i18n.savedMessage : message;
- jQuery( '#fb-success' ).text( newMessage );
- jQuery( '#fb-success' ).slideDown( 'fast' );
- setTimeout( function () {
- jQuery( '#fb-success' ).slideUp( 'fast' );
- }, 2500 );
- } catch ( e ) {
- if ( debug ) {
- console.log( 'showAndHideMessage(): ' + e );
- }
- }
- }
- function switchTabs( whichType ) {
- try {
- if ( whichType === 'preview' ) {
- if ( ! validateEmails( jQuery( '#fb-field-my-email' ).val() ) ) {
- return;
- }
- jQuery( '#tab-preview a' ).addClass( 'current' );
- jQuery( '#tab-settings a' ).removeClass( 'current' );
- jQuery( '#fb-preview-form, #fb-desc' ).show();
- jQuery( '#fb-email-settings, #fb-email-desc' ).hide();
- showAndHideMessage( GrunionFB_i18n.savedMessage );
- } else {
- jQuery( '#tab-preview a' ).removeClass( 'current' );
- jQuery( '#tab-settings a' ).addClass( 'current' );
- jQuery( '#fb-preview-form, #fb-desc, #fb-add-field' ).hide();
- jQuery( '#fb-email-settings, #fb-email-desc' ).show();
- jQuery( '#fb-field-my-email' ).focus().select();
- }
- } catch ( e ) {
- if ( debug ) {
- console.log( 'switchTabs(): ' + e );
- }
- }
- }
- function validateEmails( emails ) {
- // Field is allowed to be empty :)
- if ( 0 === emails.length ) {
- return true;
- }
-
- var $e,
- emailList = emails.split( ',' );
-
- for ( $e = 0; $e < emailList.length; $e++ ) {
- if ( false === validateEmail( emailList[ $e ] ) ) {
- alert( emailList[ $e ] + GrunionFB_i18n.invalidEmail );
- return false;
- }
- }
-
- return true;
- }
- /* Uses The Official Standard: RFC 5322 -- http://www.regular-expressions.info/email.html */
- function validateEmail( email ) {
- var re = /^(?=[a-z0-9@.!#$%&'*+/=?^_`{|}~-]{6,254}$)(?=[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}@)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}$)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
- return re.test( email );
- }
- function updateLabel() {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- var thisLabel = jQuery( '#fb-new-label' ).val();
- // Update preview
- if ( thisLabel.length === 0 ) {
- jQuery( '#fb-new-field' + thisId + ' label .label-text' ).text( GrunionFB_i18n.newLabel );
- } else {
- jQuery( '#fb-new-field' + thisId + ' label .label-text' ).text( thisLabel );
- }
- // Update fbForm object
- fbForm.fields[ thisId ].label = thisLabel;
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateLabel(): ' + e );
- }
- }
- }
- function updateMyEmail() {
- try {
- var thisEmail = jQuery( '#fb-field-my-email' ).val();
- fbForm.to = thisEmail;
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateMyEmail(): ' + e );
- }
- }
- }
- function updateOption( that ) {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- var thisOptionid = that.attr( 'optionid' );
- var thisOptionValue = that.val();
- var thisType = jQuery( '#fb-new-type' ).val();
- // Update preview
- if ( thisType === 'radio' ) {
- jQuery( '#fb-radio-' + thisId + '-' + thisOptionid + ' span' ).text( thisOptionValue );
- } else if ( 'checkbox-multiple' === thisType ) {
- jQuery( '#fb-checkbox-multiple-' + thisId + '-' + thisOptionid + ' span' ).text(
- thisOptionValue
- );
- } else {
- jQuery( '#fb-' + thisId + '-' + thisOptionid ).text( thisOptionValue );
- }
- // Update fbForm object
- fbForm.fields[ thisId ].options[ thisOptionid ] = thisOptionValue;
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateOption(): ' + e );
- }
- }
- }
- function updateRequired() {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- var thisChecked = jQuery( '#fb-new-required' ).is( ':checked' );
- // Update object and preview
- if ( thisChecked ) {
- fbForm.fields[ thisId ].required = true;
- jQuery( '#fb-new-field' + thisId + ' label' ).append(
- '
' + GrunionFB_i18n.requiredLabel + ''
- );
- } else {
- fbForm.fields[ thisId ].required = false;
- jQuery( '#fb-new-field' + thisId + ' label .label-required' ).remove();
- }
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateRequired(): ' + e );
- }
- }
- }
- function updateSubject() {
- try {
- var thisSubject = jQuery( '#fb-field-subject' ).val();
- fbForm.subject = thisSubject;
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateSubject(): ' + e );
- }
- }
- }
- function updateType( thisType, thisLabelText, thisRequired ) {
- try {
- var thisId = jQuery( '#fb-field-id' ).val();
- if ( ! thisType ) {
- thisType = jQuery( '#fb-new-type' ).val();
- }
- if ( ! thisLabelText ) {
- thisLabelText = jQuery( '#fb-new-field' + thisId + ' .label-text' ).text();
- }
- var isRequired = thisRequired
- ? '
' + GrunionFB_i18n.requiredLabel + ''
- : '';
- var thisLabel =
- '
';
- var thisRadio =
- '
';
- var thisRadioLabel =
- '
';
- var thisRadioRemove = '
';
- var thisRemove = '
';
- var thisCheckbox =
- '
';
- var thisCheckboxMultiple =
- '
';
- var thisCheckboxMultipleRemove =
- '
';
- var thisText = '
';
- var thisTextarea = '
';
- var thisClear = '
';
- var thisSelect =
- '
';
- switch ( thisType ) {
- case 'checkbox':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRadioRemove + thisCheckbox + thisRadioLabel + thisClear
- );
- break;
- case 'checkbox-multiple':
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisLabel +
- thisCheckboxMultipleRemove +
- '
'
- );
- if (
- optionsCache[ thisId ] !== undefined &&
- optionsCache[ thisId ].options.length !== 0
- ) {
- fbForm.fields[ thisId ].options = optionsCache[ thisId ].options;
- jQuery( '#fb-custom-checkbox-multiple' + thisId ).append(
- customOptions( thisId, thisType )
- );
- } else {
- jQuery( '#fb-new-options' ).html(
- '
'
- );
- jQuery( '#fb-custom-checkbox-multiple' + thisId ).append(
- '
' +
- thisCheckboxMultiple +
- '' +
- GrunionFB_i18n.firstOptionLabel +
- '' +
- thisClear +
- '
'
- );
- fbForm.fields[ thisId ].options[ optionsCount ] = GrunionFB_i18n.firstOptionLabel;
- }
- jQuery( '#fb-options' ).show();
- setTimeout( function () {
- jQuery( '#fb-option0' ).focus().select();
- }, 100 );
- break;
- case 'email':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisText
- );
- break;
- case 'name':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisText
- );
- break;
- case 'radio':
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisLabel +
- thisRadioRemove +
- '
'
- );
- if (
- optionsCache[ thisId ] !== undefined &&
- optionsCache[ thisId ].options.length !== 0
- ) {
- fbForm.fields[ thisId ].options = optionsCache[ thisId ].options;
- jQuery( '#fb-custom-radio' + thisId ).append( customOptions( thisId, thisType ) );
- } else {
- jQuery( '#fb-new-options' ).html(
- '
'
- );
- jQuery( '#fb-custom-radio' + thisId ).append(
- '
' +
- thisRadio +
- '' +
- GrunionFB_i18n.firstOptionLabel +
- '' +
- thisClear +
- '
'
- );
- fbForm.fields[ thisId ].options[ optionsCount ] = GrunionFB_i18n.firstOptionLabel;
- }
- jQuery( '#fb-options' ).show();
- setTimeout( function () {
- jQuery( '#fb-option0' ).focus().select();
- }, 100 );
- break;
- case 'select':
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisSelect
- );
- if (
- optionsCache[ thisId ] !== undefined &&
- optionsCache[ thisId ].options.length !== 0
- ) {
- fbForm.fields[ thisId ].options = optionsCache[ thisId ].options;
- jQuery( '#fb-field' + thisId ).html( customOptions( thisId, thisType ) );
- } else {
- jQuery( '#fb-new-options' ).html(
- '
'
- );
- fbForm.fields[ thisId ].options[ optionsCount ] = GrunionFB_i18n.firstOptionLabel;
- }
- jQuery( '#fb-options' ).show();
- setTimeout( function () {
- jQuery( '#fb-option0' ).focus().select();
- }, 100 );
- break;
- case 'text':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisText
- );
- break;
- case 'textarea':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisTextarea
- );
- break;
- case 'url':
- removeOptions();
- jQuery( '#fb-new-field' + thisId + ' .fb-fields' ).html(
- thisRemove + thisLabel + thisText
- );
- break;
- }
- // update object
- fbForm.fields[ thisId ].type = thisType;
- } catch ( e ) {
- if ( debug ) {
- console.log( 'updateType(): ' + e );
- }
- }
- }
- return {
- resizePop: function () {
- try {
- //Thickbox won't resize for some reason, we are manually doing it here
- var totalWidth = jQuery( 'body', window.parent.document ).width();
- var totalHeight = jQuery( 'body', window.parent.document ).height();
- var isIE6 = typeof document.body.style.maxHeight === 'undefined';
-
- jQuery( '#TB_window, #TB_iframeContent', window.parent.document ).css( 'width', '768px' );
- jQuery( '#TB_window', window.parent.document ).css( {
- left: ( totalWidth - 768 ) / 2 + 'px',
- top: '23px',
- position: 'absolute',
- marginLeft: '0',
- } );
- if ( ! isIE6 ) {
- // take away IE6
- jQuery( '#TB_window, #TB_iframeContent', window.parent.document ).css(
- 'height',
- totalHeight - 73 + 'px'
- );
- }
- } catch ( e ) {
- if ( debug ) {
- console.log( 'resizePop(): ' + e );
- }
- }
- },
- init: function () {
- // Scroll to top of page
- window.parent.scroll( 0, 0 );
- //Check for existing form data
- var contentSource;
- if (
- jQuery( '#edButtonPreview', window.parent.document ).hasClass( 'active' ) ||
- jQuery( '#wp-content-wrap', window.parent.document ).hasClass( 'tmce-active' )
- ) {
- var win = window.dialogArguments || opener || parent || top;
- contentSource = win.tinyMCE.activeEditor.getContent();
- } else {
- contentSource = jQuery( '#content', window.parent.document ).val();
- }
- var data = {
- action: 'grunion_shortcode_to_json',
- _ajax_nonce: ajax_nonce_json,
- post_id: postId,
- content: contentSource,
- };
-
- var $doc = jQuery( document );
-
- jQuery.post( ajaxurl, data, function ( response ) {
- // Setup fbForm
- parseShortcode( jQuery.parseJSON( response ) );
- // Now build out the preview form
- buildPreview();
- } );
- // actions
- jQuery( '.fb-add-field' ).click( function () {
- addField();
- hideDesc();
- return false;
- } );
- jQuery( '#fb-new-label' ).keyup( function () {
- updateLabel();
- } );
- jQuery( '#fb-new-type' ).change( function () {
- updateType();
- } );
- jQuery( '#fb-new-required' ).click( function () {
- updateRequired();
- } );
- $doc.on( 'click', '.fb-remove', function () {
- showDesc();
- deleteField( jQuery( this ) );
- grabShortcode();
- } );
- jQuery( '#fb-preview' ).submit( function () {
- sendShortcodeToEditor();
- return false;
- } );
- jQuery( '#TB_overlay, #TB_closeWindowButton', window.parent.document ).mousedown(
- function () {
- if ( confirm( GrunionFB_i18n.exitConfirmMessage ) ) {
- hidePopup();
- }
- }
- );
- $doc.on( 'click', '#fb-another-option', function () {
- addOption();
- } );
- $doc.on( 'keyup', '.fb-options', function () {
- updateOption( jQuery( this ) );
- } );
- $doc.on( 'click', '.fb-remove-option', function () {
- removeOption( jQuery( this ).attr( 'optionid' ) );
- } );
- jQuery( '#tab-preview a' ).click( function () {
- switchTabs( 'preview' );
- return false;
- } );
- jQuery( '#fb-prev-form' ).click( function () {
- switchTabs( 'preview' );
- return false;
- } );
- jQuery( '#tab-settings a' ).click( function () {
- switchTabs();
- return false;
- } );
- jQuery( '#fb-field-my-email' ).blur( function () {
- updateMyEmail();
- } );
- jQuery( '#fb-field-subject' ).blur( function () {
- updateSubject();
- } );
- $doc.on( 'mouseenter', '.fb-form-case .fb-new-fields', function () {
- hideShowEditLink( 'show', jQuery( this ) );
- } );
- $doc.on( 'mouseleave', '.fb-form-case .fb-new-fields', function () {
- hideShowEditLink( 'hide' );
- return false;
- } );
- $doc.on( 'click', '.fb-edit-field', function () {
- editField( jQuery( this ) );
- return false;
- } );
- $doc.on( 'click', '.fb-edit-field .fb-reorder', function () {
- return false;
- } );
- $doc.on( 'click', '#fb-save-field', function () {
- showDesc();
- showAndHideMessage();
- return false;
- } );
- jQuery( '#fb-feedback' ).click( function () {
- var thisHref = jQuery( this ).attr( 'href' );
- window.parent.location = thisHref;
- return false;
- } );
- jQuery( '#sortable' ).sortable( {
- axis: 'y',
- handle: '.fb-reorder',
- revert: true,
- start: function () {
- jQuery( '.fb-edit-field' ).hide();
- },
- } );
- jQuery( '#draggable' ).draggable( {
- axis: 'y',
- handle: '.fb-reorder',
- connectToSortable: '#sortable',
- helper: 'clone',
- revert: 'invalid',
- } );
- },
- };
-} )();
diff --git a/projects/plugins/jetpack/modules/contact-form/js/tinymce-plugin-form-button.js b/projects/plugins/jetpack/modules/contact-form/js/tinymce-plugin-form-button.js
deleted file mode 100644
index 01fbe3bf7f414..0000000000000
--- a/projects/plugins/jetpack/modules/contact-form/js/tinymce-plugin-form-button.js
+++ /dev/null
@@ -1,37 +0,0 @@
-/* global grunionEditorView, tinymce */
-( function () {
- tinymce.create( 'tinymce.plugins.grunion_form', {
- init: function ( editor ) {
- editor.addButton( 'grunion', {
- title: grunionEditorView.labels.tinymce_label,
- cmd: 'grunion_add_form',
- icon: 'grunion',
- } );
- editor.addCommand( 'grunion_add_form', function () {
- if ( grunionEditorView.default_form ) {
- editor.execCommand(
- 'mceInsertContent',
- 0,
- '[contact-form]' + grunionEditorView.default_form + '[/contact-form]'
- );
- } else {
- editor.execCommand( 'mceInsertContent', 0, '[contact-form /]' );
- }
- } );
- },
-
- createControl: function () {
- return null;
- },
-
- getInfo: function () {
- return {
- longname: 'Grunion Contact Form',
- author: 'Automattic',
- version: '1',
- };
- },
- } );
-
- tinymce.PluginManager.add( 'grunion_form', tinymce.plugins.grunion_form );
-} )();
diff --git a/projects/plugins/jetpack/phpunit.xml.dist b/projects/plugins/jetpack/phpunit.xml.dist
index 9cb88971c5ca7..c218e0766b39a 100644
--- a/projects/plugins/jetpack/phpunit.xml.dist
+++ b/projects/plugins/jetpack/phpunit.xml.dist
@@ -57,9 +57,6 @@
tests/php/modules/subscriptions
-
- tests/php/modules/contact-form
-
tests/php/modules/verification-tools
diff --git a/projects/plugins/jetpack/tests/php.multisite.xml b/projects/plugins/jetpack/tests/php.multisite.xml
index b786d14f71c94..2aaaf3ab7d12e 100644
--- a/projects/plugins/jetpack/tests/php.multisite.xml
+++ b/projects/plugins/jetpack/tests/php.multisite.xml
@@ -52,9 +52,6 @@
php/extensions/blocks/premium-content
-
- php/modules/contact-form
-
php/sync
php/sync/test_class.jetpack-sync-full.php
diff --git a/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php b/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php
deleted file mode 100644
index 092d3ec943a21..0000000000000
--- a/projects/plugins/jetpack/tests/php/modules/contact-form/test-class.grunion-contact-form.php
+++ /dev/null
@@ -1,2076 +0,0 @@
-set_globals();
-
- $author_id = self::factory()->user->create(
- array(
- 'user_email' => 'john@example.com',
- )
- );
-
- $post_id = self::factory()->post->create(
- array(
- 'post_status' => 'draft',
- 'post_author' => (string) $author_id,
- )
- );
-
- global $post;
- $post = get_post( $post_id );
-
- // Place post_id to contact form id to make the form processable.
- $_POST['contact-form-id'] = $post_id;
-
- // Make the global post (used by contact forms) accessbile to tests.
- $this->post = $post;
-
- // Initialize plugin.
- $this->plugin = Contact_Form_Plugin::init();
- // Call to add tokenization hook.
- $this->plugin->process_form_submission();
- }
-
- /**
- * Tears down the test environment after each unit test.
- */
- public function tear_down() {
- parent::tear_down();
-
- // Remove filters after running tests.
- remove_all_filters( 'wp_mail' );
- remove_all_filters( 'grunion_still_email_spam' );
- remove_all_filters( 'jetpack_contact_form_is_spam' );
- }
-
- /**
- * Adds the field values to the global $_POST value.
- *
- * @param array $values Array of field key value pairs.
- */
- private function add_field_values( $values ) {
- foreach ( $values as $key => $val ) {
- $_POST[ 'g' . $this->post->ID . '-' . $key ] = $val;
- }
- }
-
- /**
- * Tests that the submission as a whole will produce something in the
- * database when required information is provided.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_a_feedback_correctly_with_default_form() {
- $form = new Contact_Form( array() );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
- $email = get_post_meta( $submission->ID, '_feedback_email', true );
- $this->assertEquals( 'john ', $email['to'][0] );
- $this->assertStringContainsString( 'IP Address: 127.0.0.1', $email['message'] );
- }
-
- /**
- * Tests that the submission as a whole will produce something in the
- * database when some labels are provided.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_extra_field_metadata() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
- $extra_fields = get_post_meta( $submission->ID, '_feedback_extra_fields', true );
-
- $this->assertCount( 3, $extra_fields, 'There should be exactly three extra fields when one of the fields is name, and the others are an extra dropdown, radio button field and text field' );
-
- /*
- * Metadata starts counting from 5, because post content has:
- * 1_Name
- * 2_Dropdown
- * 3_Radio
- * 4_Text
- */
- $this->assertEquals( 'First option', $extra_fields['5_Dropdown'], 'When the first option of a dropdown field with label Dropdown is selected, there should be metadata with that key and value' );
- $this->assertEquals( 'Second option', $extra_fields['6_Radio'], 'When the first option of a radio button field with label Radio is selected, there should be metadata with that key and value' );
- $this->assertEquals( 'Texty text', $extra_fields['7_Text'], 'When the text field with label Text is filled with the text \'Texty text\', there should be metadata with that key and value' );
- }
-
- /**
- * Tests that the submission will store the subject when specified.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_subject_when_specified() {
- $form = new Contact_Form( array( 'subject' => 'I\'m sorry, but the party\'s over' ) ); // Default form.
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
-
- $this->assertStringContainsString( 'SUBJECT: I\'m sorry, but the party\'s over', $submission->post_content, 'The stored subject didn\'t match the given' );
- }
-
- /**
- * Tests that the submission will store the subject with tokens replace using the name and text fields.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_subject_with_token_replaced_from_name_and_text_field() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'state' => 'Kansas',
- )
- );
-
- $form = new Contact_Form( array( 'subject' => 'Hello {name} from {state}!' ), "[contact-field label='Name' type='name' required='1'/][contact-field label='State' type='text'/]" );
-
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
-
- $this->assertStringContainsString( 'SUBJECT: Hello John Doe from Kansas!', $submission->post_content, 'The stored subject didn\'t match the given' );
- }
-
- /**
- * Tests that the submission will store the subject with a token replaced using the radio button field.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_subject_with_token_replaced_from_radio_button_field() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'state' => 'Kansas',
- )
- );
-
- $form = new Contact_Form( array( 'subject' => 'Hello {name} from {state}!' ), "[contact-field label='Name' type='name' required='1'/][contact-field label='State' type='radio' options='Kansas,California'/]" );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
-
- $this->assertStringContainsString( 'SUBJECT: Hello John Doe from Kansas!', $submission->post_content, 'The stored subject didn\'t match the given' );
- }
-
- /**
- * Tests that the submission will store the subject with a token replaced using the dropdown field.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_subject_with_token_replaced_from_dropdown_field() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'state' => 'Kansas',
- )
- );
-
- $form = new Contact_Form( array( 'subject' => 'Hello {name} from {state}!' ), "[contact-field label='Name' type='name' required='1'/][contact-field label='State' type='select' options='Kansas,California'/]" );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
-
- $this->assertStringContainsString( 'SUBJECT: Hello John Doe from Kansas!', $submission->post_content, 'The stored subject didn\'t match the given' );
- }
-
- /**
- * Tests the form submission will store the fields and their values in the post content.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_fields_and_their_values_to_post_content() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
-
- $this->assertStringContainsString( '"1_Name":"John Doe"', $submission->post_content, 'Post content did not contain the name label and/or value' );
- $this->assertStringContainsString( '"2_Dropdown":"First option"', $submission->post_content, 'Post content did not contain the dropdown label and/or value' );
- $this->assertStringContainsString( '"3_Radio":"Second option"', $submission->post_content, 'Post content did not contain the radio button label and/or value' );
- $this->assertStringContainsString( '"4_Text":"Texty text"', $submission->post_content, 'Post content did not contain the text field label and/or value' );
- }
-
- /**
- * Tests that the form submission will store the fields and their value in the email meta.
- *
- * @author tonykova
- */
- public function test_process_submission_will_store_fields_and_their_values_to_email_meta() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form( array(), "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]" );
- $result = $form->process_submission();
-
- // Processing should be successful and produce the success message.
- $this->assertTrue( is_string( $result ) );
-
- $feedback = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $feedback, 'There should be one feedback after process_submission' );
-
- // Default metadata should be saved.
- $submission = $feedback[0];
- $email = get_post_meta( $submission->ID, '_feedback_email', true );
-
- $expected = 'Name:
John Doe
';
- $expected .= 'Dropdown:
First option
';
- $expected .= 'Radio:
Second option
';
- $expected .= 'Text:
Texty text
';
-
- $this->assertStringContainsString( $expected, $email['message'] );
- }
-
- /**
- * Tests that the form subussion sends the correct single email.
- *
- * @author tonykova
- */
- public function test_process_submission_sends_correct_single_email() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- add_filter( 'wp_mail', array( $this, 'pre_test_process_submission_sends_correct_single_email' ) );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form(
- array(
- 'to' => 'john ',
- 'subject' => 'Hello there!',
- ),
- "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]"
- );
- $result = $form->process_submission();
- $this->assertNotNull( $result );
- }
-
- /**
- * This method is hooked to the wp-mail filter.
- *
- * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
- * subject, message, headers, and attachments values.
- */
- public function pre_test_process_submission_sends_correct_single_email( $args ) {
- $this->assertContains( 'john ', $args['to'] );
- $this->assertEquals( 'Hello there!', $args['subject'] );
-
- $expected = 'Name:
John Doe
';
- $expected .= 'Dropdown:
First option
';
- $expected .= 'Radio:
Second option
';
- $expected .= 'Text:
Texty text
';
-
- $this->assertStringContainsString( $expected, $args['message'] );
- }
-
- /**
- * Tests that the response template is generated correctly
- */
- public function test_wrap_message_in_html_tags() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form(
- array(
- 'to' => 'john@example.com, jane@example.com',
- 'subject' => 'Hello there!',
- ),
- "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]"
- );
-
- $title = 'You got a new response!';
- $body = 'Here are the details:';
- $footer = 'This is the footer';
- $result = $form->wrap_message_in_html_tags( $title, $body, $footer );
-
- $this->assertStringContainsString( $title, $result );
- $this->assertStringContainsString( $body, $result );
- $this->assertStringContainsString( $footer, $result );
- }
-
- /**
- * Tests that the form subussion sends the correct multiple emails.
- *
- * @author tonykova
- */
- public function test_process_submission_sends_correct_multiple_email() {
- // Fill field values.
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- add_filter( 'wp_mail', array( $this, 'pre_test_process_submission_sends_correct_multiple_email' ) );
-
- // Initialize a form with name, dropdown and radiobutton (first, second
- // and third option), text field.
- $form = new Contact_Form(
- array(
- 'to' => 'john@example.com, jane@example.com',
- 'subject' => 'Hello there!',
- ),
- "[contact-field label='Name' type='name' required='1'/][contact-field label='Dropdown' type='select' options='First option,Second option,Third option'/][contact-field label='Radio' type='radio' options='First option,Second option,Third option'/][contact-field label='Text' type='text'/]"
- );
-
- $result = $form->process_submission();
- $this->assertNotNull( $result );
- }
-
- /**
- * This method is hooked to the wp-mail filter.
- *
- * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
- * subject, message, headers, and attachments values.
- */
- public function pre_test_process_submission_sends_correct_multiple_email( $args ) {
- $this->assertEquals( array( 'john ', 'jane ' ), $args['to'] );
- }
-
- /**
- * Tests that the form submission fails when spam is marked with a WP_Error object.
- *
- * @author tonykova
- */
- public function test_process_submission_fails_if_spam_marked_with_WP_Error() {
- add_filter( 'jetpack_contact_form_is_spam', array( $this, 'pre_test_process_submission_fails_if_spam_marked_with_WP_Error' ), 11 ); // Run after akismet filter.
-
- $form = new Contact_Form( array() );
- $result = $form->process_submission();
-
- $this->assertInstanceOf( 'WP_Error', $result, 'When $is_spam contains a WP_Error, the result of process_submission should be a WP_Error' );
- $this->assertEquals( 'Message is spam', $result->get_error_message() );
- }
-
- /**
- * This method is hooked to the jetpack_contact_form_is_spam filter.
- */
- public function pre_test_process_submission_fails_if_spam_marked_with_WP_Error() {
- return new WP_Error( 'spam', 'Message is spam' );
- }
-
- /**
- * Tests that the form submission won't send if the submission is marked as spam.
- *
- * @author tonykova
- */
- public function test_process_submission_wont_send_spam_if_marked_as_spam_with_true() {
- add_filter( 'jetpack_contact_form_is_spam', '__return_true', 11 ); // Run after akismet filter.
-
- add_filter( 'wp_mail', array( $this, 'pre_test_process_submission_wont_send_spam_if_marked_as_spam_with_true' ) );
-
- $form = new Contact_Form( array( 'to' => 'john@example.com' ) );
- $result = $form->process_submission();
- $this->assertNotNull( $result );
- }
-
- /**
- * This method is hooked to the wp-mail filter, and the test will fail if this method is called.
- */
- public function pre_test_process_submission_wont_send_spam_if_marked_as_spam_with_true() {
- $this->assertTrue( false ); // Fail if trying to send.
- }
-
- /**
- * Tests that the email subject is labeled with Spam when the submission is spam and the filter to send spam is set to true.
- *
- * @author tonykova
- */
- public function test_process_submission_labels_message_as_spam_in_subject_if_marked_as_spam_with_true_and_sending_spam() {
- add_filter( 'jetpack_contact_form_is_spam', '__return_true', 11 ); // Run after akismet filter.
-
- add_filter( 'grunion_still_email_spam', '__return_true' );
-
- add_filter( 'wp_mail', array( $this, 'pre_test_process_submission_labels_message_as_spam_in_subject_if_marked_as_spam_with_true_and_sending_spam' ) );
-
- $form = new Contact_Form( array( 'to' => 'john@example.com' ) );
- $result = $form->process_submission();
- $this->assertNotNull( $result );
- }
-
- /**
- * This method is hooked to the wp-mail filter.
- *
- * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
- * subject, message, headers, and attachments values.
- */
- public function pre_test_process_submission_labels_message_as_spam_in_subject_if_marked_as_spam_with_true_and_sending_spam( $args ) {
- $this->assertStringContainsString( '***SPAM***', $args['subject'] );
- }
-
- /**
- * Tests that 'grunion_delete_old_spam()' deletes an old post that is marked as spam.
- *
- * @author tonykova
- * @covers ::grunion_delete_old_spam
- */
- public function test_grunion_delete_old_spam_deletes_an_old_post_marked_as_spam() {
- $post_id = self::factory()->post->create(
- array(
- 'post_type' => 'feedback',
- 'post_status' => 'spam',
- 'post_date_gmt' => '1987-01-01 12:00:00',
- )
- );
-
- Util::grunion_delete_old_spam();
- $this->assertNull( get_post( $post_id ), 'An old spam feedback should be deleted' );
- }
-
- /**
- * Tests that 'grunion_delete_old_spam' does not delete a new post that is marked as spam.
- *
- * @author tonykova
- * @covers ::grunion_delete_old_spam
- */
- public function test_grunion_delete_old_spam_does_not_delete_a_new_post_marked_as_spam() {
- $post_id = self::factory()->post->create(
- array(
- 'post_type' => 'feedback',
- 'post_status' => 'spam',
- )
- );
-
- Util::grunion_delete_old_spam();
- $this->assertEquals( $post_id, get_post( $post_id )->ID, 'A new spam feedback should be left intact when deleting old spam' );
- }
-
- /**
- * Tests that token is left intact when there is not matching field.
- *
- * @author tonykova
- * @covers Contact_Form_Plugin
- */
- public function test_token_left_intact_when_no_matching_field() {
- $plugin = Contact_Form_Plugin::init();
- $subject = 'Hello {name}!';
- $field_values = array(
- 'City' => 'Chicago',
- );
-
- $this->assertEquals( 'Hello {name}!', $plugin->replace_tokens_with_input( $subject, $field_values ) );
- }
-
- /**
- * Tests that token is replaced with an empty string when there is not value in field.
- *
- * @author tonykova
- * @covers Contact_Form_Plugin
- */
- public function test_replaced_with_empty_string_when_no_value_in_field() {
- $plugin = Contact_Form_Plugin::init();
- $subject = 'Hello {name}!';
- $field_values = array(
- 'Name' => null,
- );
-
- $this->assertEquals( 'Hello !', $plugin->replace_tokens_with_input( $subject, $field_values ) );
- }
-
- /**
- * Tests that token in curly brackets is replaced with the value when the name has whitespace.
- *
- * @author tonykova
- * @covers Contact_Form_Plugin
- */
- public function test_token_can_replace_entire_subject_with_token_field_whose_name_has_whitespace() {
- $plugin = Contact_Form_Plugin::init();
- $subject = '{subject token}';
- $field_values = array(
- 'Subject Token' => 'Chicago',
- );
-
- $this->assertEquals( 'Chicago', $plugin->replace_tokens_with_input( $subject, $field_values ) );
- }
-
- /**
- * Tests that token with curly brackets is replaced with value.
- *
- * @author tonykova
- * @covers Contact_Form_Plugin
- */
- public function test_token_with_curly_brackets_can_be_replaced() {
- $plugin = Contact_Form_Plugin::init();
- $subject = '{subject {token}}';
- $field_values = array(
- 'Subject {Token}' => 'Chicago',
- );
-
- $this->assertEquals( 'Chicago', $plugin->replace_tokens_with_input( $subject, $field_values ) );
- }
-
- /**
- * Tests that the field attributes remain the same when no escaping is necessary.
- *
- * @author tonykova
- */
- public function test_parse_contact_field_keeps_string_unchanged_when_no_escaping_necesssary() {
- add_shortcode(
- 'contact-field',
- function ( $attributes, $content ) {
- return Contact_Form::parse_contact_field( $attributes, $content );
- }
- );
-
- // @phpcs:ignore Squiz.Strings.DoubleQuoteUsage.NotRequired
- $shortcode = "[contact-field label=\"Name\" type=\"name\" required=\"1\"/][contact-field label=\"Email\" type=\"email\" required=\"1\"/][contact-field label=\"asdasd\" type=\"text\"/][contact-field id=\"1\" required derp herp asd lkj]adsasd[/contact-field]";
- $html = do_shortcode( $shortcode );
-
- $this->assertEquals( $shortcode, $html );
- }
-
- /**
- * Tests that the default label is added when no label is present.
- */
- public function test_make_sure_that_we_add_default_label_when_non_is_present() {
- add_shortcode(
- 'contact-field',
- function ( $attributes, $content ) {
- return Contact_Form::parse_contact_field( $attributes, $content );
- }
- );
- $shortcode = "[contact-field type='name' required='1' /]";
- $html = do_shortcode( $shortcode );
- // @phpcs:ignore Squiz.Strings.DoubleQuoteUsage.NotRequired
- $this->assertEquals( "[contact-field type=\"name\" required=\"1\" label=\"Name\"/]", $html );
- }
-
- /**
- * Tests the empty options are removed from form fields.
- */
- public function test_make_sure_that_we_remove_empty_options_from_form_field() {
- add_shortcode(
- 'contact-field',
- function ( $attributes, $content ) {
- return Contact_Form::parse_contact_field( $attributes, $content );
- }
- );
- $shortcode = "[contact-field type='select' required='1' options='fun,,run' label='fun times' values='go,,have some fun'/]";
- $html = do_shortcode( $shortcode );
- // @phpcs:ignore Squiz.Strings.DoubleQuoteUsage.NotRequired
- $this->assertEquals( "[contact-field type=\"select\" required=\"1\" options=\"fun,run\" label=\"fun times\" values=\"go,have some fun\"/]", $html );
- }
-
- /**
- * Tests shortcode with commas and brackets.
- *
- * @covers Contact_Form_Field
- */
- public function test_array_values_with_commas_and_brackets() {
- add_shortcode(
- 'contact-field',
- function ( $attributes, $content ) {
- return Contact_Form::parse_contact_field( $attributes, $content );
- }
- );
- $shortcode = "[contact-field type='radio' options='\"foo\",bar, baz,[b\rackets]' label='fun ][ times'/]";
- $html = do_shortcode( $shortcode );
- $this->assertEquals( '[contact-field type="radio" options=""foo",bar, baz,[b\rackets]" label="fun ][ times"/]', $html );
- }
-
- /**
- * Tests Gutenblock input with commas and brackets.
- *
- * @covers Contact_Form_Field
- */
- public function test_array_values_with_commas_and_brackets_from_gutenblock() {
- $attr = array(
- 'type' => 'radio',
- 'options' => array( '"foo"', 'bar, baz', '[b\\rackets]' ),
- 'label' => 'fun ][ times',
- );
- $html = Contact_Form_Plugin::gutenblock_render_field_radio( $attr, '' );
- $this->assertEquals( '[contact-field type="radio" options=""foo",bar, baz,[b\rackets]" label="fun ][ times"/]', $html );
- }
-
- /**
- * Test for text field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_text_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'text',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'text' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for email field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_email_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'email',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'email' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for url field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_url_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'url',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'text' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for telephone field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_telephone_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'telephone',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'tel' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for date field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_date_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'date',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- 'format' => '(YYYY-MM-DD)',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'text' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for textarea field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_textarea_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'textarea',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'textarea' ) );
- $this->assertValidField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for checkbox field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_checkbox_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'checkbox',
- 'class' => 'lalala',
- 'default' => 'foo',
- 'placeholder' => 'PLACEHOLDTHIS!',
- 'id' => 'funID',
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'checkbox' ) );
- $this->assertValidCheckboxField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Multiple fields
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_checkbox_multiple_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'checkbox-multiple',
- 'class' => 'lalala',
- 'default' => 'option 1',
- 'id' => 'funID',
- 'options' => array( 'option 1', 'option 2' ),
- 'values' => array( 'option 1', 'option 2' ),
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'checkbox' ) );
- $this->assertValidFieldMultiField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for radio field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_radio_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'radio',
- 'class' => 'lalala',
- 'default' => 'option 1',
- 'id' => 'funID',
- 'options' => array( 'option 1', 'option 2', 'option 3, or 4', 'back\\slash' ),
- 'values' => array( 'option 1', 'option 2', 'option [34]', '\\' ),
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'radio' ) );
- $this->assertValidFieldMultiField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Test for select field_renders
- *
- * @covers Contact_Form_Field
- */
- public function test_make_sure_select_field_renders_as_expected() {
- $attributes = array(
- 'label' => 'fun',
- 'type' => 'select',
- 'class' => 'lalala',
- 'default' => 'option 1',
- 'id' => 'funID',
- 'options' => array( 'option 1', 'option 2', 'option 3, or 4', 'back\\slash' ),
- 'values' => array( 'option 1', 'option 2', 'option [34]', '\\' ),
- );
-
- $expected_attributes = array_merge( $attributes, array( 'input_type' => 'select' ) );
- $this->assertValidFieldMultiField( $this->render_field( $attributes ), $expected_attributes );
- }
-
- /**
- * Renders a Contact_Form_Field.
- *
- * @param array $attributes An associative array of shortcode attributes.
- *
- * @return string The field html string.
- */
- public function render_field( $attributes ) {
- $form = new Contact_Form( array() );
- $field = new Contact_Form_Field( $attributes, '', $form );
- return $field->render();
- }
-
- /**
- * Gets the first div in the input html.
- *
- * @param string $html The html string.
- *
- * @return DOMElement The first div element.
- */
- public function getCommonDiv( $html ) {
- $doc = new DOMDocument();
- $doc->loadHTML( $html );
- return $this->getFirstElement( $doc, 'div' );
- }
-
- /**
- * Gets the first element in the given DOMDocument object.
- *
- * @param DOMDocument $dom The DOMDocument object.
- * @param string $tag The tag name.
- * @param int $index The index.
- *
- * @return DOMElement The first element with the given tag.
- */
- public function getFirstElement( $dom, $tag, $index = 0 ) {
- $elements = $dom->getElementsByTagName( $tag );
- return $elements->item( $index );
- }
-
- /**
- * Tests whether the class attribute in the wrapper div matches the field's class attribute value.
- *
- * @param DOMElement $wrapper_div The wrapper div.
- * @param array $attributes An associative array containing the field's attributes.
- */
- public function assertCommonValidHtml( $wrapper_div, $attributes ) {
- if ( 'date' === $attributes['type'] ) {
- $attributes['class'] = 'jp-contact-form-date';
- }
-
- $css_class = "grunion-field-{$attributes['type']}-wrap {$attributes['class']}-wrap grunion-field-wrap";
-
- if ( 'select' === $attributes['type'] ) {
- $css_class .= ' contact-form-dropdown-wrap ui-front';
- }
-
- $this->assertEquals(
- $wrapper_div->getAttribute( 'class' ),
- $css_class,
- 'div class attribute doesn\'t match'
- );
-
- // Get label.
- $label = $this->getFirstElement( $wrapper_div, 'label' );
-
- // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $this->assertEquals( trim( $label->nodeValue ), $attributes['label'], 'Label is not what we expect it to be...' );
- }
-
- /**
- * Tests whether the class attribute in the wrapper div matches the field's class attribute value.
- *
- * @param DOMElement $wrapper_div The wrapper div.
- * @param array $attributes An associative array containing the field's attributes.
- */
- public function assertFieldClasses( $wrapper_div, $attributes ) {
- if ( 'date' === $attributes['type'] ) {
- $attributes['class'] = 'jp-contact-form-date';
- }
-
- $css_class = "grunion-field-{$attributes['type']}-wrap {$attributes['class']}-wrap grunion-field-wrap";
-
- $this->assertEquals(
- $wrapper_div->getAttribute( 'class' ),
- $css_class,
- 'div class attribute doesn\'t match'
- );
- }
-
- /**
- * Tests whether the label in the wrapper div matches the field's label.
- *
- * @param DOMElement $wrapper_div The wrapper div.
- * @param array $attributes An associative array containing the field's attributes.
- * @param string $tag_name The tag used to label the field. Could be `legend` for checkboxes
- * and radio buttons.
- */
- public function assertFieldLabel( $wrapper_div, $attributes, $tag_name = 'label' ) {
- $type = $attributes['type'];
- $label = $this->getFirstElement( $wrapper_div, $tag_name );
- $expected = 'date' === $type ? $attributes['label'] . ' ' . $attributes['format'] : $attributes['label'];
-
- // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $this->assertEquals( $expected, trim( $label->nodeValue ), 'Label is not what we expect it to be...' );
- }
-
- /**
- * Tests whether a field is valid.
- *
- * @param string $html The html string.
- * @param array $attributes An associative array containing the field's attributes.
- */
- public function assertValidField( $html, $attributes ) {
-
- $wrapper_div = $this->getCommonDiv( $html );
- $this->assertFieldClasses( $wrapper_div, $attributes );
- $this->assertFieldLabel( $wrapper_div, $attributes );
-
- // Get label.
- $label = $this->getFirstElement( $wrapper_div, 'label' );
-
- // Input.
- $input = (
- 'textarea' === $attributes['type']
- ? $this->getFirstElement( $wrapper_div, 'textarea' )
- : $this->getFirstElement( $wrapper_div, 'input' )
- );
-
- // Label matches for matches input ID.
- $this->assertEquals(
- $label->getAttribute( 'for' ),
- $input->getAttribute( 'id' ),
- 'label for does not equal input ID!'
- );
-
- $this->assertEquals( $input->getAttribute( 'placeholder' ), $attributes['placeholder'], 'Placeholder doesn\'t match' );
- if ( 'textarea' === $attributes['type'] ) {
- // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $this->assertEquals( $input->nodeValue, $attributes['default'], 'value and default doesn\'t match' );
- $this->assertEquals(
- $label->getAttribute( 'for' ),
- 'contact-form-comment-' . $input->getAttribute( 'name' ),
- 'label for doesn\'t match the input name'
- );
- } else {
- $this->assertEquals( $input->getAttribute( 'type' ), $attributes['input_type'], 'Type doesn\'t match' );
- $this->assertEquals( $input->getAttribute( 'value' ), $attributes['default'], 'value and default doesn\'t match' );
- // Label matches for matches input name.
- $this->assertEquals(
- $label->getAttribute( 'for' ),
- $input->getAttribute( 'name' ),
- 'label for doesn\'t match the input name'
- );
- }
-
- if ( 'date' === $attributes['type'] ) {
- $this->assertEquals(
- $input->getAttribute( 'class' ),
- "{$attributes['type']} jp-contact-form-date grunion-field",
- 'input class attribute doesn\'t match'
- );
- } else {
- $this->assertEquals(
- $input->getAttribute( 'class' ),
- "{$attributes['type']} {$attributes['class']} grunion-field",
- 'input class attribute doesn\'t match'
- );
- }
- }
-
- /**
- * Tests whether a checkbox field is valid.
- *
- * @param string $html The html string.
- * @param array $attributes An associative array containing the field's attributes.
- */
- public function assertValidCheckboxField( $html, $attributes ) {
-
- $wrapper_div = $this->getCommonDiv( $html );
- $this->assertFieldClasses( $wrapper_div, $attributes );
- $this->assertFieldLabel( $wrapper_div, $attributes );
-
- $label = $wrapper_div->getElementsByTagName( 'label' )->item( 0 );
- $input = $wrapper_div->getElementsByTagName( 'input' )->item( 0 );
-
- $this->assertEquals( $label->getAttribute( 'class' ), 'grunion-field-label ' . $attributes['type'], 'label class doesn\'t match' );
-
- $this->assertEquals( $input->getAttribute( 'name' ), $attributes['id'], 'Input name doesn\'t match' );
- $this->assertEquals( 'Yes', $input->getAttribute( 'value' ), 'Input value doesn\'t match' );
- $this->assertEquals( $input->getAttribute( 'type' ), $attributes['type'], 'Input type doesn\'t match' );
- if ( $attributes['default'] ) {
- $this->assertEquals( 'checked', $input->getAttribute( 'checked' ), 'Input checked doesn\'t match' );
- }
-
- $this->assertEquals( $input->getAttribute( 'class' ), $attributes['type'] . ' ' . $attributes['class'] . ' grunion-field', 'Input class doesn\'t match' );
- }
-
- /**
- * Tests whether a multifield contact form field is valid.
- *
- * @param string $html The html string.
- * @param array $attributes An associative array containing the field's attributes.
- */
- public function assertValidFieldMultiField( $html, $attributes ) {
-
- $wrapper_div = $this->getCommonDiv( $html );
- $this->assertFieldClasses( $wrapper_div, $attributes );
-
- // Inputs.
- if ( 'select' === $attributes['type'] ) {
- $label = $this->getFirstElement( $wrapper_div, 'label' );
-
- $this->assertFieldLabel( $wrapper_div, $attributes );
- $this->assertEquals( 'grunion-field-label select', $label->getAttribute( 'class' ), 'label class doesn\'t match' );
-
- $select = $this->getFirstElement( $wrapper_div, 'select' );
- $this->assertEquals(
- $label->getAttribute( 'for' ),
- $select->getAttribute( 'id' ),
- 'label for does not equal input ID!'
- );
-
- $this->assertEquals(
- $label->getAttribute( 'for' ),
- $select->getAttribute( 'name' ),
- 'label for does not equal input name!'
- );
-
- $this->assertEquals( $select->getAttribute( 'class' ), 'select ' . $attributes['class'] . ' grunion-field', ' select class does not match expected' );
-
- // Options.
- $options = $select->getElementsByTagName( 'option' );
- $n = $options->length;
- $this->assertCount( $n, $attributes['options'], 'Number of inputs doesn\'t match number of options' );
- $this->assertCount( $n, $attributes['values'], 'Number of inputs doesn\'t match number of values' );
- for ( $i = 0; $i < $n; $i++ ) {
- $option = $options->item( $i );
- $this->assertEquals( $option->getAttribute( 'value' ), $attributes['values'][ $i ], 'Input value doesn\'t match' );
- if ( 0 === $i ) {
- $this->assertEquals( 'selected', $option->getAttribute( 'selected' ), 'Input is not selected' );
- } else {
- $this->assertNotEquals( 'selected', $option->getAttribute( 'selected' ), 'Input is selected' );
- }
- //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $this->assertEquals( $option->nodeValue, $attributes['options'][ $i ], 'Input does not match the option' );
- }
- } else {
- $label = $this->getFirstElement( $wrapper_div, 'legend' );
-
- $this->assertFieldLabel( $wrapper_div, $attributes, 'legend' );
- $this->assertEquals( 'grunion-field-label', $label->getAttribute( 'class' ), 'label class doesn\'t match' );
- // Radio and Checkboxes.
- $labels = $wrapper_div->getElementsByTagName( 'label' );
- $n = $labels->length;
- $this->assertCount( $n, $attributes['options'], 'Number of inputs doesn\'t match number of options' );
- $this->assertCount( $n, $attributes['values'], 'Number of inputs doesn\'t match number of values' );
- for ( $i = 0; $i < $n; $i++ ) {
- $item_label = $labels->item( $i );
- //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $this->assertEquals( $item_label->nodeValue, $attributes['options'][ $i ] );
-
- //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
- $input = $item_label->parentNode->getElementsByTagName( 'input' )->item( 0 );
- $this->assertEquals( $input->getAttribute( 'type' ), $attributes['input_type'], 'Type doesn\'t match' );
- if ( 'radio' === $attributes['input_type'] ) {
- $this->assertEquals( $input->getAttribute( 'name' ), $attributes['id'], 'Input name doesn\'t match' );
- } else {
- $this->assertEquals( $input->getAttribute( 'name' ), $attributes['id'] . '[]', 'Input name doesn\'t match' );
- }
- $this->assertEquals( $input->getAttribute( 'value' ), $attributes['values'][ $i ], 'Input value doesn\'t match' );
- $this->assertEquals( $input->getAttribute( 'class' ), $attributes['type'] . ' ' . $attributes['class'] . ' grunion-field', 'Input class doesn\'t match' );
- if ( 0 === $i ) {
- $this->assertEquals( 'checked', $input->getAttribute( 'checked' ), 'Input checked doesn\'t match' );
- } else {
- $this->assertNotEquals( 'checked', $input->getAttribute( 'checked' ), 'Input checked doesn\'t match' );
- }
- }
- }
- }
-
- /**
- * Tests that the form attributes and values are properly escaped.
- *
- * @author tonykova
- */
- public function test_parse_contact_field_escapes_things_inside_a_value_and_attribute_and_the_content() {
- add_shortcode(
- 'contact-field',
- function ( $attributes, $content ) {
- return Contact_Form::parse_contact_field( $attributes, $content );
- }
- );
-
- $shortcode = "[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type=''email'' req'uired='1'/][contact-field label='asdasd' type='text'/][contact-field id='1' required 'derp' herp asd lkj]adsasd[/contact-field]";
- $html = do_shortcode( $shortcode );
-
- /*
- * The expected string has some quotes escaped, since we want to make
- * sure we don't output anything harmful
- */
- $this->assertEquals( '[contact-field label="Name" type="name" required="1"/][contact-field label="Email" type=''email'' req'uired='1'/][contact-field label="asdasd" type="text"/][contact-field id="1" required derp herp asd lkj]adsasd[/contact-field]', $html );
- }
-
- /**
- * Test get_export_data_for_posts with fully vaid data input.
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_get_export_data_for_posts_fully_valid_data() {
- /**
- * Contact_Form_Plugin mock object.
- *
- * @var Contact_Form_Plugin $mock
- */
- $mock = $this->getMockBuilder( Contact_Form_Plugin::class )
- ->setMethods(
- array(
- 'get_post_meta_for_csv_export',
- 'get_parsed_field_contents_of_post',
- 'get_post_content_for_csv_export',
- 'map_parsed_field_contents_of_post_to_field_names',
- 'has_json_data',
- )
- )
- ->disableOriginalConstructor()
- ->getMock();
-
- $get_post_meta_for_csv_export_map = array(
- array(
- 15,
- false,
- array(
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- 'key4' => 'value4',
-
- ),
- ),
- array(
- 16,
- false,
- array(
- 'key3' => 'value3',
- 'key4' => 'value4',
- 'key5' => 'value5',
- 'key6' => 'value6',
- ),
- ),
- );
-
- $get_parsed_field_contents_of_post_map = array(
- array( 15, array( '_feedback_subject' => 'subj1' ) ),
- array( 16, array( '_feedback_subject' => 'subj2' ) ),
- );
-
- $get_post_content_for_csv_export_map = array(
- array( 15, 'This is my test 15' ),
- array( 16, 'This is my test 16' ),
- );
-
- $mapped_fields_contents_map = array(
- array(
- array(
- '_feedback_subject' => 'subj1',
- '_feedback_main_comment' => 'This is my test 15',
- ),
- true,
- array(
- 'Contact Form' => 'subj1',
- '4_Comment' => 'This is my test 15',
- ),
- ),
- array(
- array(
- '_feedback_subject' => 'subj2',
- '_feedback_main_comment' => 'This is my test 16',
- ),
- true,
- array(
- 'Contact Form' => 'subj2',
- '4_Comment' => 'This is my test 16',
- ),
- ),
- );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_meta_for_csv_export' )
- ->willReturnMap( $get_post_meta_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_parsed_field_contents_of_post' )
- ->willReturnMap( $get_parsed_field_contents_of_post_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_content_for_csv_export' )
- ->willReturnMap( $get_post_content_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'map_parsed_field_contents_of_post_to_field_names' )
- ->willReturnMap( $mapped_fields_contents_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'has_json_data' )
- ->willReturn( false );
-
- $result = $mock->get_export_data_for_posts( array( 15, 16 ) );
-
- $expected_result = array(
- 'Contact Form' => array( 'subj1', 'subj2' ),
- 'key1' => array( 'value1', '' ),
- 'key2' => array( 'value2', '' ),
- 'key3' => array( 'value3', 'value3' ),
- 'key4' => array( 'value4', 'value4' ),
- 'key5' => array( '', 'value5' ),
- 'key6' => array( '', 'value6' ),
- '4_Comment' => array( 'This is my test 15', 'This is my test 16' ),
- );
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Test get_export_data_for_posts with single invalid entry for post meta
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_get_export_data_for_posts_invalid_single_entry_meta() {
- /**
- * Contact_Form_Plugin mock object.
- *
- * @var Contact_Form_Plugin $mock
- * */
- $mock = $this->getMockBuilder( Contact_Form_Plugin::class )
- ->setMethods(
- array(
- 'get_post_meta_for_csv_export',
- 'get_parsed_field_contents_of_post',
- 'get_post_content_for_csv_export',
- 'map_parsed_field_contents_of_post_to_field_names',
- )
- )
- ->disableOriginalConstructor()
- ->getMock();
-
- $get_post_meta_for_csv_export_map = array(
- array( 15, false, null ),
- array(
- 16,
- false,
- array(
- 'key3' => 'value3',
- 'key4' => 'value4',
- 'key5' => 'value5',
- 'key6' => 'value6',
- ),
- ),
- );
-
- $get_parsed_field_contents_of_post_map = array(
- array( 15, array( '_feedback_subject' => 'subj1' ) ),
- array( 16, array( '_feedback_subject' => 'subj2' ) ),
- );
-
- $get_post_content_for_csv_export_map = array(
- array( 15, 'This is my test 15' ),
- array( 16, 'This is my test 16' ),
- );
-
- $mapped_fields_contents_map = array(
- array(
- array(
- '_feedback_subject' => 'subj1',
- '_feedback_main_comment' => 'This is my test 15',
- ),
- true,
- array(
- 'Contact Form' => 'subj1',
- 'Comment' => 'This is my test 15',
- ),
- ),
- array(
- array(
- '_feedback_subject' => 'subj2',
- '_feedback_main_comment' => 'This is my test 16',
- ),
- true,
- array(
- 'Contact Form' => 'subj2',
- 'Comment' => 'This is my test 16',
- ),
- ),
- );
-
- // Even though there is no post meta for the first, we don't stop the cycle
- // and each mock expects two calls.
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_meta_for_csv_export' )
- ->willReturnMap( $get_post_meta_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_parsed_field_contents_of_post' )
- ->willReturnMap( $get_parsed_field_contents_of_post_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_content_for_csv_export' )
- ->willReturnMap( $get_post_content_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'map_parsed_field_contents_of_post_to_field_names' )
- ->willReturnMap( $mapped_fields_contents_map );
-
- $result = $mock->get_export_data_for_posts( array( 15, 16 ) );
-
- $expected_result = array(
- 'Contact Form' => array( 'subj1', 'subj2' ),
- 'key3' => array( '', 'value3' ),
- 'key4' => array( '', 'value4' ),
- 'key5' => array( '', 'value5' ),
- 'key6' => array( '', 'value6' ),
- 'Comment' => array( 'This is my test 15', 'This is my test 16' ),
- );
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Test get_export_data_for_posts with invalid all entries for post meta
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_get_export_data_for_posts_invalid_all_entries_meta() {
- /**
- * Contact_Form_Plugin mock object.
- *
- * @var Contact_Form_Plugin $mock
- */
- $mock = $this->getMockBuilder( Contact_Form_Plugin::class )
- ->setMethods(
- array(
- 'get_post_meta_for_csv_export',
- 'get_parsed_field_contents_of_post',
- 'get_post_content_for_csv_export',
- 'map_parsed_field_contents_of_post_to_field_names',
- )
- )
- ->disableOriginalConstructor()
- ->getMock();
-
- $get_post_meta_for_csv_export_map = array(
- array( 15, false, null ),
- array( 16, false, null ),
- );
-
- $get_parsed_field_contents_of_post_map = array(
- array( 15, array( '_feedback_subject' => 'subj1' ) ),
- array( 16, array( '_feedback_subject' => 'subj2' ) ),
- );
-
- $get_post_content_for_csv_export_map = array(
- array( 15, 'This is my test 15' ),
- array( 16, 'This is my test 16' ),
- );
-
- $mapped_fields_contents_map = array(
- array(
- array(
- '_feedback_subject' => 'subj1',
- '_feedback_main_comment' => 'This is my test 15',
- ),
- true,
- array(
- 'Contact Form' => 'subj1',
- 'Comment' => 'This is my test 15',
- ),
- ),
- array(
- array(
- '_feedback_subject' => 'subj2',
- '_feedback_main_comment' => 'This is my test 16',
- ),
- true,
- array(
- 'Contact Form' => 'subj2',
- 'Comment' => 'This is my test 16',
- ),
- ),
- );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_meta_for_csv_export' )
- ->willReturnMap( $get_post_meta_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_parsed_field_contents_of_post' )
- ->willReturnMap( $get_parsed_field_contents_of_post_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_post_content_for_csv_export' )
- ->willReturnMap( $get_post_content_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'map_parsed_field_contents_of_post_to_field_names' )
- ->willReturnMap( $mapped_fields_contents_map );
-
- $result = $mock->get_export_data_for_posts( array( 15, 16 ) );
-
- $expected_result = array(
- 'Contact Form' => array( 'subj1', 'subj2' ),
- 'Comment' => array( 'This is my test 15', 'This is my test 16' ),
- );
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Test get_export_data_for_posts with single invalid entry for parsed fields.
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_get_export_data_for_posts_single_invalid_entry_for_parse_fields() {
- /**
- * Contact_Form_Plugin mock object.
- *
- * @var Contact_Form_Plugin $mock
- * */
- $mock = $this->getMockBuilder( Contact_Form_Plugin::class )
- ->setMethods(
- array(
- 'get_post_meta_for_csv_export',
- 'get_parsed_field_contents_of_post',
- 'get_post_content_for_csv_export',
- 'map_parsed_field_contents_of_post_to_field_names',
- )
- )
- ->disableOriginalConstructor()
- ->getMock();
-
- $get_post_meta_for_csv_export_map = array(
- array(
- 15,
- false,
- array(
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- 'key4' => 'value4',
-
- ),
- ),
- array(
- 16,
- false,
- array(
- 'key3' => 'value3',
- 'key4' => 'value4',
- 'key5' => 'value5',
- 'key6' => 'value6',
- ),
- ),
- );
-
- $get_parsed_field_contents_of_post_map = array(
- array( 15, array() ),
- array( 16, array( '_feedback_subject' => 'subj2' ) ),
- );
-
- $get_post_content_for_csv_export_map = array(
- array( 15, 'This is my test 15' ),
- array( 16, 'This is my test 16' ),
- );
-
- $mapped_fields_contents_map = array(
- array(
- array(
- '_feedback_subject' => 'subj1',
- '_feedback_main_comment' => 'This is my test 15',
- ),
- true,
- array(
- 'Contact Form' => 'subj1',
- 'Comment' => 'This is my test 15',
- ),
- ),
- array(
- array(
- '_feedback_subject' => 'subj2',
- '_feedback_main_comment' => 'This is my test 16',
- ),
- true,
- array(
- 'Contact Form' => 'subj2',
- 'Comment' => 'This is my test 16',
- ),
- ),
- );
-
- $mock->expects( $this->once() )
- ->method( 'get_post_meta_for_csv_export' )
- ->willReturnMap( $get_post_meta_for_csv_export_map );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_parsed_field_contents_of_post' )
- ->willReturnMap( $get_parsed_field_contents_of_post_map );
-
- $mock->expects( $this->once() )
- ->method( 'get_post_content_for_csv_export' )
- ->willReturnMap( $get_post_content_for_csv_export_map );
-
- $mock->expects( $this->once() )
- ->method( 'map_parsed_field_contents_of_post_to_field_names' )
- ->willReturnMap( $mapped_fields_contents_map );
-
- $result = $mock->get_export_data_for_posts( array( 15, 16 ) );
-
- $expected_result = array(
- 'Contact Form' => array( 'subj2' ),
- 'key3' => array( 'value3' ),
- 'key4' => array( 'value4' ),
- 'key5' => array( 'value5' ),
- 'key6' => array( 'value6' ),
- 'Comment' => array( 'This is my test 16' ),
- );
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Test get_export_data_for_posts with all entries for parsed fields invalid.
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_get_export_data_for_posts_all_entries_for_parse_fields_invalid() {
- /**
- * Contact_Form_Plugin mock object.
- *
- * @var Contact_Form_Plugin $mock
- */
- $mock = $this->getMockBuilder( Contact_Form_Plugin::class )
- ->setMethods(
- array(
- 'get_post_meta_for_csv_export',
- 'get_parsed_field_contents_of_post',
- 'get_post_content_for_csv_export',
- 'map_parsed_field_contents_of_post_to_field_names',
- )
- )
- ->disableOriginalConstructor()
- ->getMock();
-
- $get_parsed_field_contents_of_post_map = array(
- array( 15, array() ),
- array( 16, array() ),
- );
-
- $mock->expects( $this->never() )
- ->method( 'get_post_meta_for_csv_export' );
-
- $mock->expects( $this->exactly( 2 ) )
- ->method( 'get_parsed_field_contents_of_post' )
- ->willReturnMap( $get_parsed_field_contents_of_post_map );
-
- $result = $mock->get_export_data_for_posts( array( 15, 16 ) );
-
- $expected_result = array();
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Test map_parsed_field_contents_of_post_to_field_names
- *
- * @covers Contact_Form_Plugin
- * @group csvexport
- */
- public function test_map_parsed_field_contents_of_post_to_field_names() {
-
- $input_data = array(
- 'test_field' => 'moonstruck',
- '_feedback_subject' => 'This is my form',
- '_feedback_author_email' => '',
- '_feedback_author' => 'John Smith',
- '_feedback_author_url' => 'http://example.com',
- '_feedback_main_comment' => 'This is my comment!',
- 'another_field' => 'thunderstruck',
- );
-
- $plugin = Contact_Form_Plugin::init();
-
- $result = $plugin->map_parsed_field_contents_of_post_to_field_names( $input_data );
-
- $expected_result = array(
- '1_Name' => 'John Smith',
- '3_Website' => 'http://example.com',
- '4_Comment' => 'This is my comment!',
- );
-
- $this->assertEquals( $expected_result, $result );
- }
-
- /**
- * Tests the functionality of 'Contact_Form_Plugin::personal_data_exporter'.
- *
- * @author jaswrks
- */
- public function test_personal_data_exporter() {
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'email' => 'john@example.com',
- 'dropdown' => 'First option',
- 'radio' => 'Second option',
- 'text' => 'Texty text',
- )
- );
-
- for ( $i = 1; $i <= 2; $i++ ) {
- $form = new Contact_Form(
- array(
- 'to' => '"john" ',
- 'subject' => 'Hello world! [ ' . wp_rand() . ' ]',
- ),
- '
- [contact-field label="Name" type="name" required="1"/]
- [contact-field label="Email" type="email" required="1"/]
- [contact-field label="Dropdown" type="select" options="First option,Second option,Third option"/]
- [contact-field label="Radio" type="radio" options="First option,Second option,Third option"/]
- [contact-field label="Text" type="text"/]
- '
- );
- $this->assertTrue(
- is_string( $form->process_submission() ),
- 'form submission ' . $i
- );
- }
-
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $export = $this->plugin->personal_data_exporter( 'john@example.com' );
-
- $this->assertCount( 2, $posts, 'posts count matches' );
- $this->assertCount( 2, $export['data'], 'export[data] count matches' );
-
- foreach ( $export['data'] as $data ) {
- $this->assertSame( 'feedback', $data['group_id'], 'group_id matches' );
- $this->assertSame( 'Feedback', $data['group_label'], 'group_label matches' );
- $this->assertSame( true, ! empty( $data['item_id'] ), 'has item_id key' );
- $this->assertCount( 10, $data['data'], 'has total expected data keys' );
- }
- }
-
- /**
- * Tests the functionality of 'Contact_Form_Plugin::personal_data_eraser'.
- *
- * @author jaswrks
- */
- public function test_personal_data_eraser() {
- $this->add_field_values(
- array(
- 'name' => 'John Doe',
- 'email' => 'john@example.com',
- )
- );
-
- for ( $i = 1; $i <= 2; $i++ ) {
- $form = new Contact_Form(
- array(
- 'to' => '"john" ',
- 'subject' => 'Hello world! [ ' . wp_rand() . ' ]',
- ),
- '
- [contact-field label="Name" type="name" required="1"/]
- [contact-field label="Email" type="email" required="1"/]
- '
- );
- $this->assertTrue(
- is_string( $form->process_submission() ),
- 'form submission ' . $i
- );
- }
-
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 2, $posts, 'posts count matches before erasing' );
-
- $this->plugin->personal_data_eraser( 'john@example.com' );
-
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 0, $posts, 'posts count matches after erasing' );
- }
-
- /**
- * Tests the functionality of 'Contact_Form_Plugin::personal_data_eraser' with pagination.
- */
- public function test_personal_data_eraser_pagination() {
- $this->add_field_values(
- array(
- 'name' => 'Jane Doe',
- 'email' => 'jane_doe@example.com',
- )
- );
-
- for ( $i = 1; $i <= 3; $i++ ) {
- $form = new Contact_Form(
- array(
- 'to' => '"jane" ',
- 'subject' => 'Hello world! [ ' . wp_rand() . ' ]',
- ),
- '
- [contact-field label="Name" type="name" required="1"/]
- [contact-field label="Email" type="email" required="1"/]
- '
- );
- $this->assertTrue(
- is_string( $form->process_submission() ),
- 'form submission ' . $i
- );
- }
-
- $this->add_field_values(
- array(
- 'name' => 'Jane Doe Again',
- 'email' => 'jane@example.com',
- )
- );
-
- $form = new Contact_Form(
- array(
- 'to' => '"jane" ',
- 'subject' => 'Hello world! [ ' . wp_rand() . ' ]',
- ),
- '
- [contact-field label="Name" type="name" required="1"/]
- [contact-field label="Email" type="email" required="1"/]
- '
- );
- $this->assertTrue(
- is_string( $form->process_submission() ),
- 'form submission ' . $i
- );
-
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 4, $posts, 'posts count matches before erasing' );
-
- $this->plugin->_internal_personal_data_eraser( 'jane_doe@example.com', 1, 1 );
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 3, $posts, 'posts count matches after page 1' );
-
- $this->plugin->_internal_personal_data_eraser( 'jane_doe@example.com', 2, 1 );
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 2, $posts, 'posts count matches after page 2' );
-
- $this->plugin->_internal_personal_data_eraser( 'jane_doe@example.com', 3, 1 );
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 1, $posts, 'posts count matches after page 3' );
-
- $this->plugin->_internal_personal_data_eraser( 'jane@example.com', 1, 1 );
- $posts = get_posts( array( 'post_type' => 'feedback' ) );
- $this->assertCount( 0, $posts, 'posts count matches after deleting the other feedback responder' );
- }
-
- /**
- * Tests the functionality of the grunion_contact_form_apply_block_attribute() function.
- */
- public function test_grunion_contact_form_apply_block_attribute() {
- // No contact form block.
- $original = <<
-
-
-
-
-
-
-
-
-EOT;
- $expected = <<
-
-
-
-
-
-
-
-
-EOT;
- $this->assertEquals(
- $expected,
- Util::grunion_contact_form_apply_block_attribute( $original, array( 'foo' => 'bar' ) )
- );
- // Contact form block without attributes.
- $original = <<
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-EOT;
- $expected = <<
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-EOT;
- $this->assertEquals(
- $expected,
- Util::grunion_contact_form_apply_block_attribute( $original, array( 'foo' => 'bar' ) )
- );
- // Contact form block with attributes.
- $original = <<
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-EOT;
- $expected = <<
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-EOT;
- $this->assertEquals(
- $expected,
- Util::grunion_contact_form_apply_block_attribute( $original, array( 'foo' => 'bar' ) )
- );
- }
-} // end class
diff --git a/projects/plugins/jetpack/tools/webpack.config.css.js b/projects/plugins/jetpack/tools/webpack.config.css.js
index 3c971e87e270f..465282e5bb019 100644
--- a/projects/plugins/jetpack/tools/webpack.config.css.js
+++ b/projects/plugins/jetpack/tools/webpack.config.css.js
@@ -164,9 +164,6 @@ for ( const name of [
'modules/shortcodes/css/recipes',
'modules/shortcodes/css/recipes-print',
'modules/shortcodes/css/slideshow-shortcode',
- 'modules/contact-form/css/editor-inline-editing-style',
- 'modules/contact-form/css/editor-style',
- 'modules/contact-form/css/editor-ui',
'modules/custom-css/csstidy/cssparse',
'modules/custom-css/csstidy/cssparsed',
'modules/custom-css/custom-css/css/codemirror',
@@ -190,7 +187,6 @@ for ( const name of [
// The ltr version is apparently used unminified.
for ( const name of [
'modules/carousel/jetpack-carousel',
- 'modules/contact-form/css/grunion',
'modules/related-posts/related-posts',
'modules/shortcodes/css/recipes',
'modules/shortcodes/css/recipes-print',
diff --git a/tools/eslint-excludelist.json b/tools/eslint-excludelist.json
index 8518a7a4e69eb..b0e4985cbad10 100644
--- a/tools/eslint-excludelist.json
+++ b/tools/eslint-excludelist.json
@@ -68,9 +68,6 @@
"projects/plugins/jetpack/extensions/shared/get-validated-attributes.js",
"projects/plugins/jetpack/extensions/shared/plan-upgrade-notification.js",
"projects/plugins/jetpack/modules/calypsoify/mods-gutenberg.js",
- "projects/plugins/jetpack/modules/contact-form/js/editor-view.js",
- "projects/plugins/jetpack/modules/contact-form/js/grunion-admin.js",
- "projects/plugins/jetpack/modules/contact-form/js/grunion.js",
"projects/plugins/jetpack/modules/infinite-scroll/infinity-customizer.js",
"projects/plugins/jetpack/modules/infinite-scroll/infinity.js",
"projects/plugins/jetpack/modules/plugin-search/plugin-search.js",