Skip to content
Snippets Groups Projects
Commit a37e480e authored by Josh Pollock's avatar Josh Pollock
Browse files

init commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 860 additions and 0 deletions
# single file
coverage_clover: coverage/clover.xml
service_name: travis-ci
repo_token: COVERALLS_KEY
\ No newline at end of file
# A set of files you probably don't want in your WordPress.org distribution
.distignore
.editorconfig
.git
.gitignore
.gitlab-ci.yml
.travis.yml
.DS_Store
Thumbs.db
behat.yml
bin
circle.yml
composer.json
composer.lock
Gruntfile.js
package.json
phpunit.xml
phpunit.xml.dist
multisite.xml
multisite.xml.dist
phpcs.xml
phpcs.xml.dist
README.md
wp-cli.local.yml
tests
vendor
node_modules
*.sql
*.tar.gz
*.zip
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org
# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 4
[{.jshintrc,*.json,*.yml}]
indent_style = space
indent_size = 2
[{*.txt,wp-config-sample.php}]
end_of_line = crlf
.DS_Store
Thumbs.db
wp-cli.local.yml
node_modules/
vendor/
*.sql
*.tar.gz
*.zip
.php_cs.cache
\ No newline at end of file
# Give sudo to environment
sudo: required
# Make Docker available inside the container
services:
- docker
# Use the PHP environment
language: php
# Don't send notifications via email, that's annoying
notifications:
email:
on_success: never
on_failure: never
# Trigger only on master -- we'll add Github pull requests in settings
branches:
only:
- master
# Cache composer dependencies
cache:
directories:
- vendor
- $HOME/.composer/cache
# Build these combinations of PHP and WordPress Versions
matrix:
include:
- php: 7.2
env: WP_VERSION=latest
- php: 7.1
env: WP_VERSION=latest
- php: 7.2
env: WP_VERSION=trunk
# Setup environment
before_script:
# Export composer
- export PATH="$HOME/.composer/vendor/bin:$PATH"
# Install plugin with composer
- composer install --no-progress
# Run tests
script:
# Run just unit tests first -- if they fail we never spend the time building the environment for integration tests
- composer unit-tests
# Install full test environment using composer script
- composer wp-install
# Run integration tests
- composer wp-tests
# IF tests passed run coverage and sniffs
after_sucess:
# Run coverage
- vendor/bin/phpunit --coverage-clover=coverage.xml
# Report to codecov
- bash <(curl -s https://codecov.io/bash) CODECOV_KEY
\ No newline at end of file
module.exports = function( grunt ) {
'use strict';
var banner = '/**\n * <%= pkg.homepage %>\n * Copyright (c) <%= grunt.template.today("yyyy") %>\n * This file is generated automatically. Do not edit.\n */\n';
// Project configuration
grunt.initConfig( {
pkg: grunt.file.readJSON( 'package.json' ),
addtextdomain: {
options: {
textdomain: 'caldera-forms-query',
},
update_all_domains: {
options: {
updateDomains: true
},
src: [ '*.php', '**/*.php', '!node_modules/**', '!php-tests/**', '!bin/**' ]
}
},
wp_readme_to_markdown: {
your_target: {
files: {
'README.md': 'readme.txt'
}
},
},
makepot: {
target: {
options: {
domainPath: '/languages',
mainFile: 'caldera-forms-query.php',
potFilename: 'caldera-forms-query.pot',
potHeaders: {
poedit: true,
'x-poedit-keywordslist': true
},
type: 'wp-plugin',
updateTimestamp: true
}
}
},
} );
grunt.loadNpmTasks( 'grunt-wp-i18n' );
grunt.loadNpmTasks( 'grunt-wp-readme-to-markdown' );
grunt.registerTask( 'i18n', ['addtextdomain', 'makepot'] );
grunt.registerTask( 'readme', ['wp_readme_to_markdown'] );
grunt.util.linefeed = '\n';
};
[![Build Status](https://travis-ci.org/calderawp/caldera-forms-query.svg?branch=master)](https://travis-ci.org/calderawp/caldera-forms-query)
## Development
### Install
Requires git and Composer
* `git clone git@github.com:calderawp/caldera-forms-query.git`
* `cd caldera-forms-query`
* `composer install`
### Local Development Environment
A local development environment is included, and provided. It is used for integration tests. Requires Composer, Docker and Docker Compose.
* Install Local Environment And WordPress "Unit" Test Suite
- `composer wp-install`
You should know have WordPress at http://localhost:8888/
* (re)Start Server: Once server is installed, you can start it again
- `composer wp-start`
### Testing
#### Install
Follow the steps above to create local development environment, then you can use the commands listed in the next section.
#### Use
Run these commands from the plugin's root directory.
* Run All Tests and Code Sniffs and Fixes
- `composer tests`
* Run Unit Tests
- `composer unit-tests`
* Run WordPress Integration Tests
- `composer wp-tests`
* Fix All Code Formatting
- `composer formatting`
<?php
namespace CalderaLearn\RestSearch\Tests\Integration;
/**
* Class IntegrationTestCase
*
* All integration tests MUST extend this class
*
* @package CalderaLearn\RestSearch\Tests\Integration
*/
abstract class IntegrationTestCase extends \WP_UnitTestCase
{
}
<?php
namespace CalderaLearn\RestSearch\Tests\Integration;
// phpcs:disable
/**
* Class RestAPITestCase
*
* Test case that all REST API integration tests MUST extend
*
* @package CalderaLearn\RestSearch\Tests\Integration
*/
abstract class RestAPITestCase extends IntegrationTestCase
{
/**
* Copied from \WP_Test_REST_Controller_Testcase
*
* @inheritdoc
*/
public function setUp()
{
parent::setUp();
add_filter('rest_url', array( $this, 'filter_rest_url_for_leading_slash' ), 10, 2);
/** @var \WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = new \Spy_REST_Server;
do_action('rest_api_init', $wp_rest_server);
}
/**
* Copied from \WP_Test_REST_Controller_Testcase
*
* @inheritdoc
*/
public function tearDown()
{
parent::tearDown();
remove_filter('rest_url', array( $this, 'test_rest_url_for_leading_slash' ), 10, 2);
/** @var \WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = null;
}
public function filter_rest_url_for_leading_slash($url, $path)
{
if (is_multisite()) {
return $url;
}
// Make sure path for rest_url has a leading slash for proper resolution.
$this->assertTrue(0 === strpos($path, '/'), 'REST API URL should have a leading slash.');
return $url;
}
}
// phpcs:enable
<?php
namespace CalderaLearn\RestSearch\Tests\Integration;
use CalderaLearn\RestSearch\FilterWPQuery;
use CalderaLearn\RestSearch\Tests\Mock\AlwaysFilterWPQuery;
class RestRequestTest extends RestAPITestCase
{
/**
* Ensures that REST API requests will be filtered
*
* @covers FilterWPQuery::callback()
*/
public function testShouldFilter()
{
//Create a request
$request = new \WP_REST_Request('GET', '/wp/v2/posts');
rest_api_loaded();
//Make sure the method returns true
$this->assertTrue(FilterWPQuery::shouldFilter());
}
/**
* Ensure that REST API response data was correctly altered
*
* @covers FilterWPQuery::shouldFilter();
* @covers FilterWPQuery::callback()
*/
public function testFilteringRESTRequest()
{
//Setup filter
AlwaysFilterWPQuery::addFilter();
$this->assertTrue(AlwaysFilterWPQuery::shouldFilter());
//Create a request
$request = new \WP_REST_Request('GET', '/wp/v2/posts');
//Dispatch request
$response = rest_get_server()->dispatch($request);
//Test response status
$this->assertSame(200, $response->get_status());
//Test the response data
//Use the mock data we have in our mock class as the expected values
$expected = FilterWPQuery::getPosts();
//Test that the expected results and the actual results are the same
$responseData = $response->get_data();
$this->assertTrue(is_array($responseData));
$this->assertSame(count($expected), count($responseData));
foreach ($responseData as $i => $responsePost) {
$this->assertTrue(isset($expected[$i]));
$this->assertSame($expected[$i]->post_title, $responsePost[ 'title' ][ 'rendered' ]);
}
}
}
<?php
//phpcs:disable
class wpdb
{
public function get_results( $query = null, $output = OBJECT )
{
return [
];
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit;
use calderawp\CalderaFormsQuery\EntryQueries;
use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Select\EntryValues;
use calderawp\CalderaFormsQuery\Select\SelectQueryBuilder;
class EntryQueriesTest extends TestCase
{
/**
* Test getting entry SQL generator
*
* @covers EntryQueries::getEntryGenerator()
* @covers EntryQueries::$entryGenerator
*/
public function testGetEntryGenerator()
{
$queries = $this->entryQueriesFactory();
$this->assertTrue( is_a( $queries->getEntryGenerator(), Entry::class) );
}
/**
* Test getting entry values SQL generator
*
* @covers EntryQueries::getEntryValueGenerator()
* @covers EntryQueries::$entryValueGenerator
*/public function testGetEntryValueGenerator()
{
$queries = $this->entryQueriesFactory();
$this->assertTrue( is_a( $queries->getEntryValueGenerator(), EntryValues::class) );
}
/**
* Test that getResults method returns an array
*
* @covers EntryQueries::getResults()
*/
public function testGetResults()
{
$queries = $this->entryQueriesFactory();
$this->assertTrue( is_array( $queries->getResults( "SELECT `roy` FROM sivan WHERE mike = 'roy'")));
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit;
use calderawp\CalderaFormsQuery\MySqlBuilder;
class MySqlBuilderTest extends TestCase
{
/**
* Test that sprintf tags, not :v(n) is used for substitutions
*
* @covers MySqlBuilder::$placeholderWriter
* @covers MySqlBuilder::setPlaceHolderWriter()
* @covers MySqlBuilder::__construct()
*/
public function testSubstitutions()
{
$builder = $this->MySqlBuilderFactory();
$query = new \NilPortugues\Sql\QueryBuilder\Manipulation\Select('foo');
$query
->where()
->equals( 'mike', 'roy')
;
$builder->write( $query );
$values = $builder->getValues();
$this->assertTrue(is_array( $values ) );
$this->assertArrayHasKey( '%1s', $values );
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit\Select;
use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Tests\Unit\TestCase;
class EntryTest extends TestCase
{
/**
* Test query by form ID
*
* @covers Entry::queryByFormsId()
*/
public function testQueryByFormsId()
{
$expectedSql = "SELECT `wp_cf_form_entries`.* FROM `wp_cf_form_entries` WHERE (`wp_cf_form_entries`.`form_id` = 'cf12345')";
$entryGenerator = $this->entryGeneratorFactory();
$generator = $entryGenerator->queryByFormsId( 'cf12345');
$this->assertTrue( $this->isAEntry($generator) );
$actualSql = $entryGenerator->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test query by entry ID
*
* @covers Entry::queryByEntryId()
*/
public function testQueryByEntryId()
{
$expectedSql = "SELECT `wp_cf_form_entries`.* FROM `wp_cf_form_entries` WHERE (`wp_cf_form_entries`.`id` = '42')";
$entryGenerator = $this->entryGeneratorFactory();
$generator = $entryGenerator->queryByEntryId( 42);
$this->assertTrue( $this->isAEntry($generator) );
$actualSql = $entryGenerator->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test query by user ID
*
* @covers Entry::queryByUserId()
*/
public function testQueryByUserId()
{
$expectedSql = "SELECT `wp_cf_form_entries`.* FROM `wp_cf_form_entries` WHERE (`wp_cf_form_entries`.`user_id` = '42')";
$entryGenerator = $this->entryGeneratorFactory();
$generator = $entryGenerator->queryByUserId( 42);
$this->assertTrue( $this->isAEntry($generator) );
$actualSql = $entryGenerator->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* @param $generator
* @return bool
*/
protected function isAEntry($generator)
{
return is_a($generator, '\calderawp\CalderaFormsQuery\Select\Entry');
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit\Select;
use calderawp\CalderaFormsQuery\Tests\Unit\TestCase;
class EntryValuesTest extends TestCase
{
/**
* Test query by field where field value equals a value
*
* @covers \calderawp\CalderaFormsQuery\Select\EntryValues::queryByFieldValue()
*/
public function testQueryByFieldValueEquals()
{
$expectedSql = "SELECT `cf_form_entry_values`.* FROM `cf_form_entry_values` WHERE (`cf_form_entry_values`.`value` = 'josh@calderawp.com') AND (`cf_form_entry_values`.`slug` = 'email_address')";
$entryValues = $this->entryValuesGeneratorFactory();
$generator = $entryValues->queryByFieldValue( 'email_address', 'josh@calderawp.com' );
$this->assertTrue($this->isAEntryValues($generator));
$actualSql = $entryValues->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test query by field where field value does not equals a value
*
* @covers \calderawp\CalderaFormsQuery\Select\EntryValues::queryByFieldValue()
*/
public function testQueryByFieldValueNotEquals()
{
$expectedSql = "SELECT `cf_form_entry_values`.* FROM `cf_form_entry_values` WHERE (`cf_form_entry_values`.`value` <> 'josh@calderawp.com') AND (`cf_form_entry_values`.`slug` = 'email_address')";
$entryValues = $this->entryValuesGeneratorFactory();
$generator =$entryValues->queryByFieldValue( 'email_address', 'josh@calderawp.com','notEquals' );
$this->assertTrue($this->isAEntryValues($generator));
$actualSql = $entryValues->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test query by field where field value is like a value
*
* @cover \calderawp\CalderaFormsQuery\Select\EntryValues::$isLike
* @covers \calderawp\CalderaFormsQuery\Select\EntryValues::queryByFieldValue()
*/
public function testQueryByFieldValueLike()
{
$expectedSql = "SELECT `cf_form_entry_values`.* FROM `cf_form_entry_values` WHERE (`cf_form_entry_values`.`value` LIKE %josh@calderawp.com%)";
$entryValues = $this->entryValuesGeneratorFactory();
$generator = $entryValues->queryByFieldValue( 'email_address', 'josh@calderawp.com','like' );
$this->assertTrue($this->isAEntryValues($generator));
$actualSql = $entryValues->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test query by entry id
*
* @covers \calderawp\CalderaFormsQuery\Select\EntryValues::queryByFieldValue()
*/
public function testQueryByEntryId()
{
$expectedSql = "SELECT `cf_form_entry_values`.* FROM `cf_form_entry_values` WHERE (`cf_form_entry_values`.`entry_id` = '42')";
$entryValues = $this->entryValuesGeneratorFactory();
$generator = $entryValues->queryByEntryId( 42 );
$this->assertTrue($this->isAEntryValues($generator));
$actualSql = $entryValues->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* @param $generator
* @return bool
*/
protected function isAEntryValues($generator)
{
return is_a($generator, '\calderawp\CalderaFormsQuery\Select\EntryValues');
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit\Select;
use calderawp\CalderaFormsQuery\MySqlBuilder;
use calderawp\CalderaFormsQuery\Select\Entry;
use calderawp\CalderaFormsQuery\Select\EntryValues;
use calderawp\CalderaFormsQuery\Select\SelectQueryBuilder;
use calderawp\CalderaFormsQuery\Tests\Unit\TestCase;
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
class SelectQueryBuilderTest extends TestCase
{
/**
* Test table names
*
* @covers SelectQueryBuilder::getTableName()
* @covers SelectQueryBuilder::$tableName
*/
public function testGetTableName()
{
$entry = $this->entryGeneratorFactory();
$this->assertEquals( 'wp_cf_form_entries', $entry->getTableName() );
$entryValues = $this->entryValuesGeneratorFactory();
$this->assertSame( 'cf_form_entry_values', $entryValues->getTableName() );
}
/**
* Test getting SQL builder
*
* @covers SelectQueryBuilder::getBuilder()
* @covers SelectQueryBuilder::$builder
*/
public function testGetBuilder()
{
$entry = $this->entryGeneratorFactory();
$this->assertTrue( is_a( $entry->getBuilder(), MySqlBuilder::class ) );
$entryValues = $this->entryValuesGeneratorFactory();
$this->assertTrue( is_a( $entryValues->getBuilder(), MySqlBuilder::class ) );
}
/**
* Test getting select query generator
*
* @covers SelectQueryBuilder::getSelectQuery()
* @covers SelectQueryBuilder::$query
*/
public function testGetSelectQuery()
{
$entry = $this->entryGeneratorFactory();
$this->assertTrue( is_a( $entry->getSelectQuery(), Select::class ) );
$entryValues = $this->entryValuesGeneratorFactory();
$this->assertTrue( is_a( $entryValues->getSelectQuery(), Select::class ) );
}
/**
* Test adding orderby DESC
*
* @covers SelectQueryBuilder::addOrderBy()
*/
public function testAddOrderByDesc()
{
$entry = $this->entryGeneratorFactory();
$expectedSql = "SELECT `wp_cf_form_entries`.* FROM `wp_cf_form_entries` WHERE (`wp_cf_form_entries`.`form_id` = 'cf12345') ORDER BY `wp_cf_form_entries`.`form_id` DESC";
$entry->queryByFormsId( 'cf12345' );
$entry->addOrderBy( 'form_id', false );
$actualSql = $entry->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test adding orderby ASC
*
* @covers SelectQueryBuilder::addOrderBy()
*/
public function testAddOrderByAsc()
{
$entry = $this->entryGeneratorFactory();
$expectedSql = "SELECT `wp_cf_form_entries`.* FROM `wp_cf_form_entries` WHERE (`wp_cf_form_entries`.`form_id` = 'cf12345') ORDER BY `wp_cf_form_entries`.`form_id` ASC";
$entry->queryByFormsId( 'cf12345' );
$entry->addOrderBy( 'form_id' );
$actualSql = $entry->getPreparedSql();
$this->assertEquals( $expectedSql, $actualSql );
}
/**
* Test the ASC constant
*
* @covers SelectQueryBuilder::ASC
*/
public function testAscConstant()
{
$this->assertEquals( Entry::ASC, 'ASC' );
$this->assertEquals( EntryValues::ASC, 'ASC' );
}
/**
* Test the SDESC constant
*
* @covers SelectQueryBuilder::DESC
*/
public function testDescConstant()
{
$this->assertEquals( Entry::DESC, 'DESC' );
$this->assertEquals( EntryValues::DESC, 'DESC' );
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaFormsQuery\Tests\Unit;
//Import PHP unit test case.
//Must be aliased to avoid having two classes of same name in scope.
use calderawp\CalderaFormsQuery\EntryQueries;
use PHPUnit\Framework\TestCase as FrameworkTestCase;
/**
* Class TestCase
*
* Default test case for all unit tests
* @package CalderaLearn\RestSearch\Tests\Unit
*/
abstract class TestCase extends FrameworkTestCase
{
/**
* @return \calderawp\CalderaFormsQuery\Select\EntryValues
*/
protected function entryValuesGeneratorFactory()
{
return new \calderawp\CalderaFormsQuery\Select\EntryValues(
$this->MySqlBuilderFactory(),
'cf_form_entry_values'
);
}
/**
* @return \calderawp\CalderaFormsQuery\Select\Entry
*/
protected function entryGeneratorFactory()
{
return new \calderawp\CalderaFormsQuery\Select\Entry(
$this->MySqlBuilderFactory(),
'wp_cf_form_entries'
);
}
/**
* @return \calderawp\CalderaFormsQuery\MySqlBuilder
*/
protected function MySqlBuilderFactory(): \calderawp\CalderaFormsQuery\MySqlBuilder
{
return new \calderawp\CalderaFormsQuery\MySqlBuilder();
}
/**
* @return EntryQueries
*/
protected function entryQueriesFactory()
{
return new EntryQueries(
$this->entryGeneratorFactory(),
$this->entryValuesGeneratorFactory(),
new \wpdb()
);
}
}
<?php
// phpcs:disable
/**
* This is the bootstrap file for Integration Tests -- run with composer wp-tests
*/
$_tests_dir = getenv('WP_TESTS_DIR');
if (! $_tests_dir) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin()
{
require dirname(dirname(__FILE__)) . '/caldera-forms-query.php';
}
tests_add_filter('muplugins_loaded', '_manually_load_plugin');
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
// phpcs:enable
<?php
// phpcs:disable
/**
* This is the bootstrap file for Unit Tests -- run using composer unit-tests
*/
//Manually include plugin
include_once dirname(__FILE__, 2) . '/caldera-forms-query.php';
/** Translation compatibility */
if (! function_exists('translate')) {
/**
* @param string $text
* @return string mixed
*/
function translate($text)
{
return $text;
}
}
if (! function_exists('__')) {
/**
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* Default 'default'.
* @return string Translated text.
*/
function __($text, $domain = 'default')
{
return translate($text, $domain);
}
}
if ( ! defined( 'ARRAY_A')) {
define('OBJECT', 'OBJECT');
define('object', 'OBJECT');
define('OBJECT_K', 'OBJECT_K');
define('ARRAY_A', 'ARRAY_A');
define('ARRAY_N', 'ARRAY_N');
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment