diff --git a/phpunit.xml b/phpunit.xml index 9ddd900..858f617 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -35,5 +35,10 @@ tests/TestSuites/Editor + + + tests/TestSuites/ElasticSearch + + diff --git a/src/NF.php b/src/NF.php index 9d46fe8..4ccbb5f 100644 --- a/src/NF.php +++ b/src/NF.php @@ -1,53 +1,63 @@ log($text, $label); + Console::getInstance() + ->log($text, $label); } /** @@ -206,7 +203,7 @@ private static function initGuzzle($config) /** * Returns a Search instance * - * @return Search + * @return ElasticSearch */ public static function search() { diff --git a/src/Netflex/Site/Console.php b/src/Netflex/Site/Console.php index 3cc633b..c1ca28a 100644 --- a/src/Netflex/Site/Console.php +++ b/src/Netflex/Site/Console.php @@ -45,17 +45,4 @@ public function log ($text, $labels = null) { $this->handler->debug($text, $labels); } } - - /** - * @deprecated 1.0.11 - * - * @param string $text - * @param string $label - * @return void - */ - public function debug ($text, $label) - { - trigger_error('NF::$console->debug is deprecated', E_USER_DEPRECATED); - return $this->log($text, $label); - } } diff --git a/src/Netflex/Site/ElasticSearch.php b/src/Netflex/Site/ElasticSearch.php index 5e1d877..06b2d74 100644 --- a/src/Netflex/Site/ElasticSearch.php +++ b/src/Netflex/Site/ElasticSearch.php @@ -10,13 +10,16 @@ */ class ElasticSearch { + /** @var array */ + private $terms = []; - private $_result = null; - private $_sort = []; - private $_index = 'entry'; - private $_terms = []; + /** @var array|null */ + private $result = null; - private $_query = [ + /** @var bool */ + private $isRawSearch = false; + + private $query = [ 'index' => 'entry', '_source' => [], 'body' => [ @@ -27,22 +30,35 @@ class ElasticSearch ] ]; - /** Defines legal relation types */ - protected $_relations = [ - 'page', - 'entry', - 'order', - 'signup', - 'customer', - ]; + /** @var string */ + const PAGE = 'page'; + + /** @var string */ + const ENTRY = 'entry'; + + /** @var string */ + const ORDER = 'order'; + + /** @var string */ + const SIGNUP = 'signup'; + + /** @var string */ + const CUSTOMER = 'customer'; + /** + * Perform a Lucenene query + * + * @param string $string + * @param bool $or + * @return static + */ public function query($string = '', $or = false) { - if (!isset($this->_terms['query'])) { - $this->_terms['query'] = []; + if (!isset($this->terms['query'])) { + $this->terms['query'] = []; } - $this->_terms['query'][] = [ + $this->terms['query'][] = [ 'key' => 'query', 'param' => $string, 'match' => 'must', @@ -54,39 +70,39 @@ public function query($string = '', $or = false) } /** - * Sets the relation for Search + * Sets the relation * - * @params string $relation - * @return Search + * @param string $relation + * @return static */ public function relation($relation) { - $this->_query['index'] = $relation; + $this->query['index'] = $relation; return $this; } /** - * Adds a directory to the search query + * Adds a directory constraint * - * @param string|int $directory - * @return Search + * @param int $directory + * @return static */ public function directory($directory = null) { - $this->_query['index'] = 'entry_' . $directory; + $this->query['index'] = 'entry_' . $directory; $this->equals('directory_id', $directory); return $this; } /** - * Excludes a directory from the search query + * Excludes a directory * - * @param string|int $directory - * @return Search + * @param int $directory + * @return static */ public function notDirectory($directory = null) { - $this->_query['index'] = 'entry'; + $this->query['index'] = 'entry'; $this->notEquals('directory_id', $directory); return $this; } @@ -94,21 +110,31 @@ public function notDirectory($directory = null) /** * Overrides the query string * - * @params string $query Raw qery string - * @return Search + * @param string $query Raw qery string + * @return static */ public function raw($query) { - $this->_query = $query; + $this->query = $query; + $this->isRawSearch = true; return $this; } - private function buildQuery() + /** + * Compiles the query + * + * @return array + */ + public function buildQuery() { + if ($this->isRawSearch) { + return $this->query; + } + $query = []; $previousTerm = null; $previousNode = null; - foreach ($this->_terms as $field => $terms) { + foreach ($this->terms as $field => $terms) { $terms = count($terms) ? $terms : [$terms]; foreach ($terms as $term) { @@ -137,9 +163,9 @@ private function buildQuery() unset($query[$previousTerm['match']]['query_string']); } } else if (isset($previousTerm['param']['must_not'])) { - array_pop($query[$previousTerm['match']][$previous['type']]); - if (!count($query[$previousTerm['match']][$previous['type']])) { - unset($query[$previousTerm['match']][$previous['type']]); + array_pop($query[$previousTerm['match']][$previousTerm['type']]); + if (!count($query[$previousTerm['match']][$previousTerm['type']])) { + unset($query[$previousTerm['match']][$previousTerm['type']]); } } else { array_pop($query[$previousTerm['match']]); @@ -157,62 +183,67 @@ private function buildQuery() } } - $this->_query['body']['query'] = ['bool' => $query]; + $this->query['body']['query'] = ['bool' => $query]; + + return $this->query; } /** * Performs the actual search with the built query * - * @params bool $fetch Fetches the search results - * @params bool $debug Returns debug info for the search - * @params array $order Sort key and order of the search - * @params string $limit Limit results on search - * @params string $from From result item for pagination - * @params bool $count Returns the number of hits for the search - * @params bool $json Returns the results as an Object - * @return mixed + * @throws Exception + * @return void */ - private function execute($debug = false) + private function execute() { + if (!get_setting('use_elasticsearch')) { + throw new Exception('ElasticSearch is not enabled for this site'); + } + $this->buildQuery(); - NF::debug(json_encode($this->_query, JSON_PRETTY_PRINT), 'ElasticSearchQuery'); + NF::debug(json_encode($this->query, JSON_PRETTY_PRINT), 'ElasticSearch'); - $url = 'search/raw'; try { - $result = NF::$capi - ->post($url, ['json' => $this->_query]) - ->getBody(); - $this->_result = json_decode( + $result = NF::$capi->post('search/raw', [ + 'json' => $this->query + ])->getBody(); + + $this->result = json_decode( str_replace('##D##', '-', json_encode(json_decode($result))) ); } catch (Exception $ex) { - $this->_result = json_decode( + $this->result = json_decode( json_encode(['hits' => ['total' => 0]]) ); - NF::$console->debug($ex); - throw new Exception(json_encode($this->_query)); + + NF::debug($ex, 'ElasticSearch'); + throw new Exception(json_encode($this->query)); } } /** * Builds a partial query string * - * @params string $key The property to query - * @params string $param The value to query - * @params string $type The bool type type - * @return string + * @param string $key The property to query + * @param string $param The value to query + * @param string $type The bool type type + * @param string $match + * @param bool $or = false + * @return static */ public function where($key, $param, $type = 'term', $match = 'must', $or = false) { $key = str_replace('-', '##D##', $key); + if (is_null($param)) { $type = 'bool'; - /* $type = 'filter'; */ - $param = [$match === 'must_not' ? 'must' : 'must_not' => ['exists' => ['field' => $key]]]; + $match_type = $match === 'must_not'? 'must' : 'must_not'; + $param = [$match_type => ['exists' => ['field' => $key]]]; } - $this->_terms[$key] = !isset($this->_terms[$key]) ? [] : $this->_terms[$key]; - $this->_terms[$key][] = [ + + $this->terms[$key] = $this->terms[$key] ?? []; + $this->terms[$key][] = [ 'key' => $key, 'param' => $param, 'type' => $type, @@ -226,9 +257,10 @@ public function where($key, $param, $type = 'term', $match = 'must', $or = false /** * Search where key not contains the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function notContains($key, $param, $or = false) { @@ -239,9 +271,10 @@ public function notContains($key, $param, $or = false) /** * Search where key contains the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function contains($key, $param, $or = false) { @@ -251,9 +284,10 @@ public function contains($key, $param, $or = false) /** * Search where key equals the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function equals($key, $param, $or = false) { @@ -263,9 +297,10 @@ public function equals($key, $param, $or = false) /** * Search where key not equals the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function notEquals($key, $param, $or = false) { @@ -275,9 +310,10 @@ public function notEquals($key, $param, $or = false) /** * Search where key is less than the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function lessThan($key, $param, $or = false) { @@ -288,9 +324,10 @@ public function lessThan($key, $param, $or = false) /** * Search where key is less than the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function lessThanOrEqual($key, $param, $or = false) { @@ -301,9 +338,10 @@ public function lessThanOrEqual($key, $param, $or = false) /** * Search where key in range of $from -> $to value * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function range($key, $from, $to, $or = false) { @@ -314,9 +352,10 @@ public function range($key, $from, $to, $or = false) /** * Search where key is greater than the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function greaterThan($key, $param, $or = false) { @@ -327,9 +366,10 @@ public function greaterThan($key, $param, $or = false) /** * Search where key is greater than the given param * - * @params string $key - * @params mixed $param - * @return Search + * @param string $key + * @param mixed $param + * @param bool $or = false + * @return static */ public function greaterThanOrEqual($key, $param, $or = false) { @@ -338,26 +378,26 @@ public function greaterThanOrEqual($key, $param, $or = false) } /** - * Number of items to return + * Max items to return * - * @params string $limit - * @return Search + * @param string $limit + * @return static */ public function limit($limit) { - $this->_query['size'] = $limit; + $this->query['size'] = $limit; return $this; } /** * Return records from this item for pagination * - * @params string $from - * @return Search + * @param string $from + * @return static */ public function from($from) { - $this->_query['from'] = $from; + $this->query['from'] = $from; return $this; } @@ -378,7 +418,7 @@ public function fetch() $this->execute(); return array_map(function ($hit) { return $hit->_source; - }, $this->_result->hits->hits); + }, $this->result->hits->hits); } /** @@ -388,7 +428,7 @@ public function fetch() */ public function getRawResults() { - return $this->_result; + return $this->result; } /** @@ -398,27 +438,26 @@ public function getRawResults() */ public function debug() { - $this->buildQuery(); - return $this->_query; + return $this->buildQuery(); } /** * Specifies a field to get * - * @params string $item Field name - * @return Search + * @param string $item Field name + * @return static */ public function field($item) { - $this->_query['_source'][] = $item; + $this->query['_source'][] = $item; return $this; } /** * Specifies multiple fields to get * - * @params array $items Array of field names - * @return Search + * @param array $items Array of field names + * @return static */ public function fields($items = []) { @@ -431,14 +470,14 @@ public function fields($items = []) /** * Sorts the search by the given key and direction * - * @params string $key Field name - * @params string $dir Direction - * @return Search + * @param string $key Field name + * @param string $dir Direction + * @return static */ public function sortBy($key = 'updated', $dir = 'asc') { $dir = !in_array($dir, ['asc', 'desc']) ? 'asc' : $dir; - $this->_query['body']['sort'][] = [$key => $dir]; + $this->query['body']['sort'][] = [$key => $dir]; return $this; } @@ -449,10 +488,10 @@ public function sortBy($key = 'updated', $dir = 'asc') */ public function count() { - $_source = $this->_query['_source']; - $this->_query['_source'] = ['id']; + $_source = $this->query['_source']; + $this->query['_source'] = ['id']; $this->execute(); - $this->_query['_source'] = $_source; - return $this->_result->hits->total; + $this->query['_source'] = $_source; + return $this->result->hits->total; } } diff --git a/src/Netflex/Site/Search.php b/src/Netflex/Site/Search.php deleted file mode 100644 index 5bad57e..0000000 --- a/src/Netflex/Site/Search.php +++ /dev/null @@ -1,423 +0,0 @@ -post( - 'elasticsearch/raw', - ['json' => $query] - )->getBody(); - - return json_decode($result); - } - - /** - * Adds a directory to the search query - * - * @param string|int $directory - * @return Search - */ - public function directory($directory = null) - { - if (!is_numeric($directory)) { - $directory = $this->mapDirectory($directory); - } - if ($directory) { - $this->equals('directory_id', $directory); - } - return $this; - } - - /** - * Excludes a directory from the search query - * - * @param string|int $directory - * @return Search - */ - public function notDirectory($directory = null) - { - if (!is_numeric($directory)) { - $directory = $this->mapDirectory($directory); - } - if ($directory) { - $this->notEquals('directory_id', $directory); - } - return $this; - } - - /** - * Maps a directory name to a id - * - * @param string $directoryName - * @return int - */ - private function mapDirectory($directoryName) - { - if (!$this->_directories) { - $this->_directories = json_decode(NF::$capi->get('builder/structures')->getBody()); - } - $found = null; - foreach ($this->_directories as $directory) { - if (strtolower($directory->name) === strtolower($directoryName)) { - $found = intval($directory->id); - break; - } - } - return $found; - } - - /** - * Returns the current query string - * - * @params void - * @return string The current query string - */ - public function getQueryString() - { - return trim($this->_query); - } - - /** - * Returns the full post payload - * - * @params void - * @return array The current post payload - */ - public function buildQuery() - { - return [ - 'terms' => $this->getQueryString(), - 'relation' => $this->_relation, - 'fetch' => $this->_fetch, - 'order' => $this->_orderBy, - 'debug' => $this->_debug, - 'limit' => $this->_limit, - 'from' => $this->_from, - 'count' => $this->_count, - 'fields' => $this->_fields ? $this->_fields : null - ]; - } - - /** - * Sets the relation for Search - * - * @params string $relation - * @return Search - */ - public function relation($relation) - { - if (!in_array($relation, $this->relations)) { - throw new Exception('Invalid relation type: ' . $relation); - } - $this->_relation = $relation; - return $this; - } - - /** - * Overrides the query string - * - * @params string $query Raw qery string - * @return Search - */ - public function raw($query) - { - $this->_query = $query; - return $this; - } - - /** - * Performs the actual search with the built query - * - * @params bool $fetch Fetches the search results - * @params bool $debug Returns debug info for the search - * @params array $order Sort key and order of the search - * @params string $limit Limit results on search - * @params string $from From result item for pagination - * @params bool $count Returns the number of hits for the search - * @params bool $json Returns the results as an Object - * @return mixed - */ - private function execute($fetch = false, $debug = false, $count = false, $json = true) - { - $url = 'search' . ($this->_relation ? '/' . $this->_relation : ''); - $result = NF::$capi - ->post($url, ['json' => $this->buildQuery()]) - ->getBody(); - return $json ? json_decode($result) : $result; - } - - /** - * Builds a partial query string - * - * @params string $key The property to query - * @params string $param The search query for the $key - * @params string $type The query operation to perform - * @return string - */ - public function where($key, $param, $type = ':') - { - return $key . $type . $param . ' '; - } - - /** - * Search where key not contains the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function notContains($key, $param) - { - $this->_query .= '-' . $this->where($key, $param, ':'); - return $this; - } - - /** - * Search where key contains the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function contains($key, $param) - { - $this->_query .= $this->where($key, $param, ':'); - return $this; - } - - /** - * Search where key equals the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function equals($key, $param) - { - $this->_query .= $this->where($key, $param, '::'); - return $this; - } - - /** - * Search where key not equals the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function notEquals($key, $param) - { - $this->_query .= '-' . $this->where($key, $param, '::'); - return $this; - } - - /** - * Search where key is less than the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function lessThan($key, $param) - { - $this->_query .= $this->where($key, $param, ':<'); - return $this; - } - - /** - * Search where key is greater than the given param - * - * @params string $key - * @params mixed $param - * @return Search - */ - public function greaterThan($key, $param) - { - $this->_query .= $this->where($key, $param, ':>'); - return $this; - } - - /** - * Number of items to return - * - * @params string $limit - * @return Search - */ - public function limit($limit) - { - $this->_limit = $limit; - return $this; - } - - /** - * Return records from this item for pagination - * - * @params string $from - * @return Search - */ - public function from($from) - { - $this->_from = $from; - return $this; - } - - /** - * Removes unused fields from output - * - * @param array $fetched - * @return array - */ - private function formatOutput($fetched) - { - foreach ($fetched as $result) { - foreach ($result as $key => $value) { - if (!in_array($key, $this->_fields)) { - unset($result->{$key}); - } - } - } - return $fetched; - } - - /** - * Returns the search results - * - * @return mixed - */ - public function fetch() - { - $this->_fetch = true; - $fetched = $this->execute(); - if ($this->_fields) { - $fetched = $this->formatOutput($fetched); - } - return $fetched; - } - - /** - * Returns the ES debug info - * - * @return mixed - */ - public function debug() - { - $this->_debug = true; - return $this->execute(); - } - - /** - * Specifies a field to get - * - * @params string $item Field name - * @return Search - */ - public function field($item) - { - $this->_fields[] = $item; - return $this; - } - - /** - * Specifies multiple fields to get - * - * @params array $items Array of field names - * @return Search - */ - public function fields($items = []) - { - foreach ($items as $item) { - $this->field($item); - } - return $this; - } - - /** - * Sorts the search by the given key and direction - * - * @params string $key Field name - * @params string $dir Direction - * @param int $sort Sort method - * @return Search - */ - public function sortBy($key = 'total_score', $dir = 'desc', $sort = SORT_STRING) - { - $this->_orderBy[$key] = [$sort, $dir == 'desc' ? SORT_DESC : SORT_ASC]; - return $this; - } - - - /** - * Returns the search results meta data - * - * @return mixed - */ - public function get() - { - return $this->execute(); - } - - /** - * Returns the number of found entries - * - * @return int - */ - public function count() - { - $this->_json = false; - $this->_count = true; - return intval($this->execute()); - } - - /** - * Returns debug info - * - * @return array - */ - public function __debugInfo() - { - return $this->buildQuery(); - } - - /** - * Casts instance to string - * - * @return string - */ - public function __toString() - { - return $this->getQueryString(); - } -} diff --git a/src/build_error.php b/src/build_error.php index 6145974..427fec6 100644 --- a/src/build_error.php +++ b/src/build_error.php @@ -13,16 +13,16 @@ if (!$isAsset) { - $logtype = convert_to_safe_string('page', 'text'); - $loglink = convert_to_safe_string($url, 'text'); - $logcode = convert_to_safe_string('404', 'int'); - $logref = convert_to_safe_string('none', 'text'); + $logtype = 'page'; + $loglink = strip_tags($url); + $logcode = 404; + $logref = 'none'; if (isset($_SERVER['HTTP_REFERER'])) { - $logref = convert_to_safe_string($_SERVER['HTTP_REFERER'], 'text'); + $logref = strip_tags($_SERVER['HTTP_REFERER']); } - $logagent = convert_to_safe_string($_SERVER['HTTP_USER_AGENT'], 'text'); + $logagent = strip_tags($_SERVER['HTTP_USER_AGENT']); //Set header http_response_code(404); diff --git a/src/controller_page.php b/src/controller_page.php index 6eee715..68112f6 100644 --- a/src/controller_page.php +++ b/src/controller_page.php @@ -10,7 +10,7 @@ // Get and clean url for security $real_url = explode('?', urldecode($_GET['_path']))[0]; -$url = trim(convert_to_safe_string($real_url, 'long'), '/'); +$url = trim($real_url, '/'); if (strpos($url, '_/') === 0) { global $url_asset; @@ -40,7 +40,7 @@ $url = $url . '/'; // Get full url for checking redirects -$fullUrl = ltrim(convert_to_safe_string($_SERVER['REQUEST_URI'], 'long'), '/'); +$fullUrl = ltrim($_SERVER['REQUEST_URI'], '/'); // Log url and full url NF::debug($url, 'Path'); diff --git a/src/functions.php b/src/functions.php index 8ea52fa..cb51f02 100644 --- a/src/functions.php +++ b/src/functions.php @@ -23,9 +23,3 @@ require_once($filename); } -if (function_exists('get_setting')) { - if (get_setting('use_elasticsearch')) { - NF::$search = new Netflex\Site\ElasticSearch(); - NF::debug('Using ElasticSearch', 'info'); - } -} diff --git a/src/functions/common/functions_blocks.php b/src/functions/common/functions_blocks.php index 9429fbe..e35f5c8 100644 --- a/src/functions/common/functions_blocks.php +++ b/src/functions/common/functions_blocks.php @@ -33,7 +33,7 @@ function get_page_blocks($area, $vars = null) } foreach ($content as $section) { - $block = convert_to_safe_string($section['text'], 'int'); + $block = $section['text']; $alias = NF::$site->templates['components'][$block]['alias']; $blockhash = $section['title']; NF::debug('components/' . $alias, 'component'); @@ -80,7 +80,7 @@ function display_page_blocks($page_id, $area, $vars = null) NF::$site->content = get_full_content_array($page_id); foreach ($content as $section) { - $block = convert_to_safe_string($section['text'], 'int'); + $block = $section['text']; $alias = NF::$site->templates['components'][$block]['alias']; $blockhash = $section['title']; NF::debug('components/' . $alias, 'component'); diff --git a/src/functions/common/functions_convert.php b/src/functions/common/functions_convert.php index d51deb9..480a107 100644 --- a/src/functions/common/functions_convert.php +++ b/src/functions/common/functions_convert.php @@ -20,6 +20,7 @@ function convert_datetime($date, $format) /** * Security and conversion variables * + * @deprecated v1.1.0 * @param string $value * @param string $type * @param string $definedValue = "" @@ -28,6 +29,8 @@ function convert_datetime($date, $format) */ function convert_to_safe_string($value, $type, $definedValue = "", $undefinedValue = "") { + trigger_error('convert_to_safe_string is deprecated', E_USER_DEPRECATED); + switch ($type) { case 'text': $value = ($value != '') ? "'" . $value . "'" : 'null'; diff --git a/src/functions/common/functions_customers.php b/src/functions/common/functions_customers.php index 144b709..0452a02 100644 --- a/src/functions/common/functions_customers.php +++ b/src/functions/common/functions_customers.php @@ -9,7 +9,7 @@ */ function get_customer_data($username, $data = null) { - $data = convert_to_safe_string($data, 'str'); + $data = strip_tags($data); try { $customer = json_decode(NF::$capi->get('relations/customers/customer/resolve/' . $username)->getBody(), true); @@ -32,7 +32,6 @@ function get_customer_data($username, $data = null) */ function get_group_members($group) { - $group = convert_to_safe_string($group, 'int'); $request = NF::$capi->get('relations/customers/groups/' . $group . '/customers'); $members = json_decode($request->getBody(), true); return $members; @@ -47,8 +46,6 @@ function get_group_members($group) */ function add_customer_to_group($customer, $group) { - $customer = convert_to_safe_string($customer, 'int'); - $group = convert_to_safe_string($group, 'int'); $request = NF::$capi->put('relations/customers/customer/' . $customer, ['json' => ['groups' => $group]]); return 1; } @@ -61,8 +58,6 @@ function add_customer_to_group($customer, $group) */ function delete_customer($user_hash) { - $user_hash = convert_to_safe_string($user_hash, 'text'); - if ($user_hash) { // Check that customer exists $customer = json_decode(NF::$capi->get('relations/customers/customer/hash/' . $user_hash)->getBody(), true); diff --git a/src/functions/common/functions_directories.php b/src/functions/common/functions_directories.php index fc17923..37926ce 100644 --- a/src/functions/common/functions_directories.php +++ b/src/functions/common/functions_directories.php @@ -11,7 +11,6 @@ function get_directory_entry($id) global $entry_override; global $revision_override; - $id = convert_to_safe_string($id, 'int'); $entrydata = (isset($entry_override) && $entry_override == $id && isset($revision_override)) ? null : NF::$cache->fetch("entry/$id"); if ($entrydata == null) { @@ -186,8 +185,6 @@ function get_latest_entries($directory_id, $orderlimit) */ function get_full_directory($directory_id, $order = []) { - - $directory_id = convert_to_safe_string($directory_id, 'int'); $data = NF::$cache->fetch("structure/$directory_id/entries"); if ($data == null) { $request = NF::$capi->get('builder/structures/' . $directory_id . '/entries'); diff --git a/src/functions/common/functions_index.php b/src/functions/common/functions_index.php index 9d825a8..8d3d856 100644 --- a/src/functions/common/functions_index.php +++ b/src/functions/common/functions_index.php @@ -12,14 +12,8 @@ */ function search($terms, $relation = null, $limit = 0, $orderby = null, $cacheKey = null) { - $relation = convert_to_safe_string($relation, 'str'); - $limit = convert_to_safe_string($limit, 'int'); - NF::debug(['terms' => $terms, 'relation' => $relation, 'limit' => $limit, 'orderby' => $orderby], 'Search terms'); - // Make query ready for processing - $terms = convert_to_safe_string($terms, 'str'); - if ($relation) { $endpoint = 'search/' . $relation; } else { @@ -53,10 +47,6 @@ function search($terms, $relation = null, $limit = 0, $orderby = null, $cacheKey */ function delete_index($relation, $relation_id) { - // Clean values - $relation = convert_to_safe_string($relation, 'text'); - $relation_id = convert_to_safe_string($relation_id, 'int'); - try { NF::$capi->delete('search/purge/item/' . $relation . '/' . $relation_id); return 1; diff --git a/src/functions/common/functions_nav.php b/src/functions/common/functions_nav.php index f2e31c3..403bde3 100644 --- a/src/functions/common/functions_nav.php +++ b/src/functions/common/functions_nav.php @@ -30,8 +30,6 @@ function get_nav_sub_ids($page_id, $visibility = null) { $ids = []; - $page_id = convert_to_safe_string($page_id, 'int'); - $visibility = convert_to_safe_string($visibility, 'str'); $pages = NF::$site->pages; if ($visibility == null) { foreach ($pages as $pg) { diff --git a/src/functions/common/functions_store.php b/src/functions/common/functions_store.php index dafe3be..970e224 100644 --- a/src/functions/common/functions_store.php +++ b/src/functions/common/functions_store.php @@ -17,14 +17,7 @@ function get_order_id() */ function get_order_secret() { - if (isset($_SESSION['netflex_cart'])) { - $secret = convert_to_safe_string($_SESSION['netflex_cart'], 'str'); - } else if (isset($_COOKIE['netflex_cart'])) { - $secret = convert_to_safe_string($_COOKIE['netflex_cart'], 'str'); - } else { - $secret = null; - } - return $secret; + return $_SESSION['netflex_cart'] ?? $_COOKIE['netflex_cart'] ?? null; } /** @@ -52,11 +45,13 @@ function get_order($id) /** * In Stock * - * @deprecated 1.1.0 + * @deprecated v1.1.0 * @return int */ function in_stock() { + trigger_error('in_stock is deprecated', E_USER_DEPRECATED); + return 1; } @@ -193,8 +188,6 @@ function get_customer_orders($customer_id) */ function start_checkout($order_id) { - // Clean data - $order_id = convert_to_safe_string($order_id, 'int'); $user_ip = get_client_ip(); $user_agent = $_SERVER['HTTP_USER_AGENT']; diff --git a/src/functions/common/functions_system.php b/src/functions/common/functions_system.php index de16f02..e8721d7 100644 --- a/src/functions/common/functions_system.php +++ b/src/functions/common/functions_system.php @@ -42,8 +42,8 @@ function get_redirect($url, $data) NF::$cache->save("redirects", $redirects); } - $current_host = convert_to_safe_string($_SERVER['HTTP_HOST'], 'str'); - $current_host_slash = convert_to_safe_string($_SERVER['HTTP_HOST'], 'str') . '/'; + $current_host = $_SERVER['HTTP_HOST']; + $current_host_slash = $_SERVER['HTTP_HOST'] . '/'; if (count($redirects)) { foreach ($redirects as $item) { @@ -214,7 +214,7 @@ function random_string($length) function send_notification($subject, $body, $receivers = [], $log = true, $template = null, $reply_to = null) { // Clean values - $subject = convert_to_safe_string($subject, 'str'); + $subject = strip_tags($subject); $body = base64_encode($body); // Set receivers diff --git a/src/functions/editor/functions_blocks.php b/src/functions/editor/functions_blocks.php index 7536290..cf0e83c 100644 --- a/src/functions/editor/functions_blocks.php +++ b/src/functions/editor/functions_blocks.php @@ -13,35 +13,33 @@ function set_edit_btn($settings = [], $position = 'topright') global $page_id; global $_mode; - if ($_mode === 'preview') { - return; - } - - if (!is_null($settings['alias'])) { - $settings['alias'] = $settings['alias'] . '_' . $blockhash; - $settings['max-items'] = $settings['max-items'] ?? 99999; - - $position = $position ?? 'topright'; - - if (!is_null($settings['config'])) { - $config = base64_encode(serialize($settings['config'])); - } - - $class = convert_to_safe_string($settings['class'], 'str'); - $style = convert_to_safe_string($settings['style'], 'str'); - - if ($settings['label'] != null) { - $btntitle = $settings['label']; - } else { - $btntitle = $settings['name']; + if ($_mode !== 'preview') { + if (!is_null($settings['alias'])) { + $settings['alias'] = $settings['alias'] . '_' . $blockhash; + $settings['max-items'] = $settings['max-items'] ?? 99999; + $position = $position ?? 'topright'; + $config = $settings['config'] ? base64_encode(serialize($settings['config'])) : null; + $class = strip_tags($settings['class']); + $style = strip_tags($settings['style']); + $btntitle = $settings['label'] ?? $settings['name']; + + return << + {$btntitle} + +HTML; } - - if ($settings['icon'] != null) { - $btnicon = ' '; - } else { - $btnicon = null; - } - - return '' . $btnicon . '' . $btntitle . ''; } } diff --git a/src/functions/live/functions_auth.php b/src/functions/live/functions_auth.php index 04dff74..d26ba2d 100644 --- a/src/functions/live/functions_auth.php +++ b/src/functions/live/functions_auth.php @@ -7,12 +7,11 @@ * @param string $password * @param string $groups = '99999' * @param string $field = 'mail' + * @return int */ function check_login($username, $password, $groups = '99999', $field = 'mail') { - $username = convert_to_safe_string(strtolower($username), 'str'); - $password = convert_to_safe_string($password, 'str'); - $groups = convert_to_safe_string($groups, 'str'); + $username = strtolower($username); if ($groups == '99999') { $input = ['username' => $username, 'password' => $password, 'field' => $field]; @@ -56,21 +55,25 @@ function check_login($username, $password, $groups = '99999', $field = 'mail') function check_access($username = null, $groups = null) { if ($username != null || $groups = null) { - $username = convert_to_safe_string($username, 'str'); - try { - $customer = json_decode(NF::$capi->get('relations/customers/customer/resolve/' . $username)->getBody(), true); - } catch (Exception $e) { - return 0; - } + $customer = json_decode( + NF::$capi->get('relations/customers/customer/resolve/' . $username) + ->getBody(), + true + ); - if (isset($customer['id']) && $groups == '99999') { - return 1; - } else if (isset($customer['id']) && in_array($group, $customer['groups'])) { - return 1; - } else if (isset($customer['id'])) { - return 2; - } + if (isset($customer['id']) && $groups == '99999') { + return 1; + } + + if (isset($customer['id']) && in_array($groups, $customer['groups'])) { + return 1; + } + + if (isset($customer['id'])) { + return 2; + } + } catch (Exception $e) {} return 0; } diff --git a/src/model/StructureQueryPage.php b/src/model/StructureQueryPage.php index 25ddcd6..5bd0b15 100644 --- a/src/model/StructureQueryPage.php +++ b/src/model/StructureQueryPage.php @@ -7,25 +7,25 @@ trait DeprecatedFieldsInStructureQueryPage { /** - * @deprecated + * @deprecated v1.1.0 * @var int */ public $last_page; /** - * @deprecated + * @deprecated v1.1.0 * @var int|null */ public $next_page; /** - * @deprecated + * @deprecated v1.1.0 * @var int */ public $total_items; /** - * @deprecated + * @deprecated v1.1.0 * @var int|null */ public $items_per_page; diff --git a/tests/TestSuites/Editor/functions_blocks/__snapshots__/Editor_SetEditBtnTest__testSetEditBtn__1.html b/tests/TestSuites/Editor/functions_blocks/__snapshots__/Editor_SetEditBtnTest__testSetEditBtn__1.html index b9b19cb..8e4cd5c 100644 --- a/tests/TestSuites/Editor/functions_blocks/__snapshots__/Editor_SetEditBtnTest__testSetEditBtn__1.html +++ b/tests/TestSuites/Editor/functions_blocks/__snapshots__/Editor_SetEditBtnTest__testSetEditBtn__1.html @@ -1,3 +1,5 @@ -Test \ No newline at end of file + + Test + \ No newline at end of file diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..260f685 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "match": { + "test": "hello" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..9a03c8f --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchContainsTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must_not": [ + { + "match": { + "test": "hello" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..ac42cd0 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,18 @@ +{ + "index": "entry_10000", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "term": { + "directory_id": 10000 + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testNotOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testNotOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..a893c28 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchDirectoryTest__testNotOutputsMatchesSnapshot__1.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must_not": [ + { + "term": { + "directory_id": 10000 + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..d157572 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "term": { + "test": "hello" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..0a1c52b --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchEqualsTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must_not": [ + { + "term": { + "test": "hello" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..b4f5d9d --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,12 @@ +{ + "index": "entry", + "_source": [ + "field1" + ], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..6fba6ca --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFieldTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,14 @@ +{ + "index": "entry", + "_source": [ + "field1", + "field2", + "field3" + ], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFromTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFromTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..d7f9aa5 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchFromTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,11 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + }, + "from": 10 +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..34cd3ea --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,20 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "range": { + "test": { + "gt": 10 + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..0ce02d0 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchGreaterThanTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,20 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "range": { + "test": { + "gte": 10 + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..610ab47 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,20 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "range": { + "test": { + "lt": 10 + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..94f2525 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLessThanTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,20 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "range": { + "test": { + "lte": 10 + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLimitTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLimitTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..48de28d --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchLimitTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,11 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + }, + "size": 10 +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchPaginateTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchPaginateTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..1f43762 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchPaginateTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,12 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + }, + "from": 10, + "size": 100 +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRangeTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRangeTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..ffe850e --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRangeTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,21 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "range": { + "test": { + "gte": 10, + "lte": 20 + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__1.json new file mode 100644 index 0000000..76655d0 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__1.json @@ -0,0 +1,14 @@ +{ + "index": "test", + "_source": [ + "id", + "field1", + "field2" + ], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__2.json new file mode 100644 index 0000000..4fedcc0 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__2.json @@ -0,0 +1,15 @@ +{ + "index": "test", + "_source": [ + "id", + "field1", + "field2" + ], + "body": { + "sort": [], + "query": { + "bool": [] + } + }, + "size": 10 +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__3.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__3.json new file mode 100644 index 0000000..003061a --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRawTest__testOutputsMatchesSnapshots__3.json @@ -0,0 +1,15 @@ +{ + "index": "test2", + "_source": [ + "id", + "field1", + "field2" + ], + "body": { + "sort": [], + "query": { + "bool": [] + } + }, + "size": 10 +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__1.json new file mode 100644 index 0000000..9f73466 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__1.json @@ -0,0 +1,10 @@ +{ + "index": "page", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__2.json new file mode 100644 index 0000000..4371d9b --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__2.json @@ -0,0 +1,10 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__3.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__3.json new file mode 100644 index 0000000..e2231e1 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__3.json @@ -0,0 +1,10 @@ +{ + "index": "order", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__4.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__4.json new file mode 100644 index 0000000..8c09c10 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__4.json @@ -0,0 +1,10 @@ +{ + "index": "signup", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__5.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__5.json new file mode 100644 index 0000000..4d78ce1 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchRelationTest__testOutputsMatchesSnapshots__5.json @@ -0,0 +1,10 @@ +{ + "index": "customer", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__1.json new file mode 100644 index 0000000..184bb17 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__1.json @@ -0,0 +1,14 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [ + { + "test": "asc" + } + ], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__2.json new file mode 100644 index 0000000..184bb17 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__2.json @@ -0,0 +1,14 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [ + { + "test": "asc" + } + ], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__3.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__3.json new file mode 100644 index 0000000..f6ba378 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchSortByTest__testDirectoryOutputsMatchesSnapshot__3.json @@ -0,0 +1,14 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [ + { + "test": "desc" + } + ], + "query": { + "bool": [] + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__1.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__1.json new file mode 100644 index 0000000..8f5bf23 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__1.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "term": { + "test": "abc" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__2.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__2.json new file mode 100644 index 0000000..cab1407 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__2.json @@ -0,0 +1,22 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "must": [ + { + "bool": { + "must_not": { + "exists": { + "field": "test" + } + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__3.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__3.json new file mode 100644 index 0000000..d138e49 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__3.json @@ -0,0 +1,18 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "fgh": [ + { + "cde": { + "test": "abc" + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__4.json b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__4.json new file mode 100644 index 0000000..3cac495 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/__snapshots__/ElasticSearchWhereTest__testOutputsMatchesSnapshots__4.json @@ -0,0 +1,22 @@ +{ + "index": "entry", + "_source": [], + "body": { + "sort": [], + "query": { + "bool": { + "fgh": [ + { + "bool": { + "must_not": { + "exists": { + "field": "test" + } + } + } + } + ] + } + } + } +} diff --git a/tests/TestSuites/ElasticSearch/es.contains.test.php b/tests/TestSuites/ElasticSearch/es.contains.test.php new file mode 100644 index 0000000..71cbe36 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.contains.test.php @@ -0,0 +1,51 @@ +contains('test', 'hello'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->notContains('test', 'hello'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testExpectMissingOrClauseException(): void + { + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->contains('test', 'hello', true); + $search->buildQuery(); + + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->notContains('test', 'hello', true); + $search->buildQuery(); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.directory.test.php b/tests/TestSuites/ElasticSearch/es.directory.test.php new file mode 100644 index 0000000..22ac1bc --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.directory.test.php @@ -0,0 +1,41 @@ +directory(10000); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testNotOutputsMatchesSnapshot(): void + { + $search = new ElasticSearch; + $search->notDirectory(10000); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.equals.test.php b/tests/TestSuites/ElasticSearch/es.equals.test.php new file mode 100644 index 0000000..70630c3 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.equals.test.php @@ -0,0 +1,51 @@ +equals('test', 'hello'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->notEquals('test', 'hello'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testExpectMissingOrClauseException(): void + { + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->equals('test', 'hello', true); + $search->buildQuery(); + + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->notEquals('test', 'hello', true); + $search->buildQuery(); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.field.test.php b/tests/TestSuites/ElasticSearch/es.field.test.php new file mode 100644 index 0000000..06f09e1 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.field.test.php @@ -0,0 +1,38 @@ +field('field1'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->fields(['field1', 'field2', 'field3']); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.from.test.php b/tests/TestSuites/ElasticSearch/es.from.test.php new file mode 100644 index 0000000..476f904 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.from.test.php @@ -0,0 +1,30 @@ +from(10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.greater.test.php b/tests/TestSuites/ElasticSearch/es.greater.test.php new file mode 100644 index 0000000..2ec0952 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.greater.test.php @@ -0,0 +1,51 @@ +greaterThan('test', 10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->greaterThanOrEqual('test', 10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testExpectMissingOrClauseException(): void + { + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->greaterThan('test', 10, true); + $search->buildQuery(); + + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->greaterThanOrEqual('test', 10, true); + $search->buildQuery(); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.less.test.php b/tests/TestSuites/ElasticSearch/es.less.test.php new file mode 100644 index 0000000..f306fc1 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.less.test.php @@ -0,0 +1,51 @@ +lessThan('test', 10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->lessThanOrEqual('test', 10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testExpectMissingOrClauseException(): void + { + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->lessThan('test', 10, true); + $search->buildQuery(); + + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->lessThanOrEqual('test', 10, true); + $search->buildQuery(); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.limit.test.php b/tests/TestSuites/ElasticSearch/es.limit.test.php new file mode 100644 index 0000000..a7d6371 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.limit.test.php @@ -0,0 +1,30 @@ +limit(10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.paginate.test.php b/tests/TestSuites/ElasticSearch/es.paginate.test.php new file mode 100644 index 0000000..bb23021 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.paginate.test.php @@ -0,0 +1,30 @@ +paginate(10, 100); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.range.test.php b/tests/TestSuites/ElasticSearch/es.range.test.php new file mode 100644 index 0000000..cc0c7e4 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.range.test.php @@ -0,0 +1,38 @@ +range('test', 10, 20); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } + + public function testExpectMissingOrClauseException(): void + { + $this->expectException('Exception'); + $search = new ElasticSearch; + $search->range('test', 10, 20, true); + $search->buildQuery(); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.raw.test.php b/tests/TestSuites/ElasticSearch/es.raw.test.php new file mode 100644 index 0000000..3555eb6 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.raw.test.php @@ -0,0 +1,57 @@ +relation(ElasticSearch::PAGE); + + $query = [ + 'index' => 'test', + '_source' => ['id', 'field1', 'field2'], + 'body' => [ + 'sort' => [], + 'query' => [ + 'bool' => [] + ] + ] + ]; + + $search->raw($query); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->limit(10); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->relation('test2'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.relation.test.php b/tests/TestSuites/ElasticSearch/es.relation.test.php new file mode 100644 index 0000000..da81610 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.relation.test.php @@ -0,0 +1,58 @@ +relation(ElasticSearch::PAGE); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->relation(ElasticSearch::ENTRY); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->relation(ElasticSearch::ORDER); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->relation(ElasticSearch::SIGNUP); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search->relation(ElasticSearch::CUSTOMER); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.sortby.test.php b/tests/TestSuites/ElasticSearch/es.sortby.test.php new file mode 100644 index 0000000..3c0949c --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.sortby.test.php @@ -0,0 +1,46 @@ +sortBy('test'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->sortBy('test', 'asc'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->sortBy('test', 'desc'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +} diff --git a/tests/TestSuites/ElasticSearch/es.where.test.php b/tests/TestSuites/ElasticSearch/es.where.test.php new file mode 100644 index 0000000..5f47079 --- /dev/null +++ b/tests/TestSuites/ElasticSearch/es.where.test.php @@ -0,0 +1,54 @@ +where('test', 'abc'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->where('test', null); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->where('test', 'abc', 'cde', 'fgh'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + + $search = new ElasticSearch; + $search->where('test', null, 'cde', 'fgh'); + + $this->assertMatchesSnapshot( + $search->buildQuery(), + new JsonDriver + ); + } +}