class QueryTest

Same name in this branch
  1. 9 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php \Drupal\Tests\views\Kernel\Plugin\QueryTest
  2. 9 core/modules/views_ui/tests/src/Functional/QueryTest.php \Drupal\Tests\views_ui\Functional\QueryTest
  3. 9 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php \Drupal\KernelTests\Core\Database\QueryTest
  4. 9 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php \Drupal\Tests\Core\Entity\Query\Sql\QueryTest
Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Plugin/QueryTest.php \Drupal\Tests\views\Kernel\Plugin\QueryTest
  2. 8.9.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php \Drupal\views_test_data\Plugin\views\query\QueryTest
  3. 8.9.x core/modules/views_ui/tests/src/Functional/QueryTest.php \Drupal\Tests\views_ui\Functional\QueryTest
  4. 8.9.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php \Drupal\KernelTests\Core\Database\QueryTest
  5. 8.9.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php \Drupal\Tests\Core\Entity\Query\Sql\QueryTest
  6. 10 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php \Drupal\Tests\views\Kernel\Plugin\QueryTest
  7. 10 core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php \Drupal\views_test_data\Plugin\views\query\QueryTest
  8. 10 core/modules/views_ui/tests/src/Functional/QueryTest.php \Drupal\Tests\views_ui\Functional\QueryTest
  9. 10 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php \Drupal\KernelTests\Core\Database\QueryTest
  10. 10 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php \Drupal\Tests\Core\Entity\Query\Sql\QueryTest
  11. 11.x core/modules/views/tests/src/Kernel/Plugin/QueryTest.php \Drupal\Tests\views\Kernel\Plugin\QueryTest
  12. 11.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php \Drupal\views_test_data\Plugin\views\query\QueryTest
  13. 11.x core/modules/views_ui/tests/src/Functional/QueryTest.php \Drupal\Tests\views_ui\Functional\QueryTest
  14. 11.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php \Drupal\KernelTests\Core\Database\QueryTest
  15. 11.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php \Drupal\Tests\Core\Entity\Query\Sql\QueryTest

Defines a query test plugin.

Plugin annotation


@ViewsQuery(
  id = "query_test",
  title = @Translation("Query test"),
  help = @Translation("Defines a query test plugin.")
)

Hierarchy

Expanded class hierarchy of QueryTest

1 file declares its use of QueryTest
QueryTest.php in core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
1 string reference to 'QueryTest'
QueryTest::calculateDependencies in core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
Calculates dependencies for the configured plugin.

File

core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php, line 20

Namespace

Drupal\views_test_data\Plugin\views\query
View source
class QueryTest extends QueryPluginBase {
    protected $conditions = [];
    protected $fields = [];
    protected $allItems = [];
    protected $orderBy = [];
    
    /**
     * {@inheritdoc}
     */
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['test_setting'] = [
            'default' => '',
        ];
        return $options;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['test_setting'] = [
            '#title' => $this->t('Test setting'),
            '#type' => 'textfield',
            '#default_value' => $this->options['test_setting'],
        ];
    }
    
    /**
     * Sets the allItems property.
     *
     * @param array $allItems
     *   An array of stdClasses.
     */
    public function setAllItems($allItems) {
        $this->allItems = $allItems;
    }
    public function addWhere($group, $field, $value = NULL, $operator = NULL) {
        $this->conditions[] = [
            'field' => $field,
            'value' => $value,
            'operator' => $operator,
        ];
    }
    public function addField($table, $field, $alias = '', $params = []) {
        $this->fields[$field] = $field;
        return $field;
    }
    public function addOrderBy($table, $field = NULL, $order = 'ASC', $alias = '', $params = []) {
        $this->orderBy = [
            'field' => $field,
            'order' => $order,
        ];
    }
    public function ensureTable($table, $relationship = NULL, JoinPluginBase $join = NULL) {
        // There is no concept of joins.
    }
    
    /**
     * Implements Drupal\views\Plugin\views\query\QueryPluginBase::build().
     *
     * @param \Drupal\views\ViewExecutable $view
     *   The view executable.
     */
    public function build(ViewExecutable $view) {
        $this->view = $view;
        // @todo Support pagers for know, a php based one would probably match.
        // @todo You could add a string representation of the query.
        $this->view->build_info['query'] = "";
        $this->view->build_info['count_query'] = "";
    }
    
    /**
     * {@inheritdoc}
     */
    public function execute(ViewExecutable $view) {
        $result = [];
        foreach ($this->allItems as $element) {
            // Run all conditions on the element, and add it to the result if they
            // match.
            $match = TRUE;
            foreach ($this->conditions as $condition) {
                $match &= $this->match($element, $condition);
            }
            if ($match) {
                // If the query explicit defines fields to use, filter all others out.
                // Filter out fields
                if ($this->fields) {
                    $element = array_intersect_key($element, $this->fields);
                }
                $result[] = new ResultRow($element);
            }
        }
        $this->view->result = $result;
    }
    
    /**
     * Check a single condition for a single element.
     *
     * @param array $element
     *   The element which should be checked.
     * @param array $condition
     *   An associative array containing:
     *   - field: The field to by, for example id.
     *   - value: The expected value of the element.
     *   - operator: The operator to compare the element value with the expected
     *     value.
     *
     * @return bool
     *   Returns whether the condition matches with the element.
     */
    public function match($element, $condition) {
        $value = $element[$condition['field']];
        switch ($condition['operator']) {
            case '=':
                return $value == $condition['value'];
            case 'IN':
                return in_array($value, $condition['value']);
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function calculateDependencies() {
        return parent::calculateDependencies() + [
            'content' => [
                'QueryTest',
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function setFieldTimezoneOffset(&$field, $offset) {
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DerivativeInspectionInterface::getBaseId public function Gets the base_plugin_id of the plugin instance. 1
DerivativeInspectionInterface::getDerivativeId public function Gets the derivative_id of the plugin instance. 1
PluginBase::$definition public property Plugins' definition.
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$renderer protected property Stores the render API renderer. 3
PluginBase::$usesOptions protected property Denotes whether the plugin has an additional options form. 8
PluginBase::$view public property The top object of a view. 1
PluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 63
PluginBase::destroy public function Clears a plugin. Overrides ViewsPluginInterface::destroy 2
PluginBase::doFilterByDefinedOptions protected function Do the work to filter out stored options depending on the defined options.
PluginBase::filterByDefinedOptions public function Filter out stored options depending on the defined options. Overrides ViewsPluginInterface::filterByDefinedOptions
PluginBase::getAvailableGlobalTokens public function Returns an array of available token replacements. Overrides ViewsPluginInterface::getAvailableGlobalTokens
PluginBase::getProvider public function Returns the plugin provider. Overrides ViewsPluginInterface::getProvider
PluginBase::getRenderer protected function Returns the render API renderer. 1
PluginBase::globalTokenForm public function Adds elements for available core tokens to a form. Overrides ViewsPluginInterface::globalTokenForm
PluginBase::globalTokenReplace public function Returns a string with any core tokens replaced. Overrides ViewsPluginInterface::globalTokenReplace
PluginBase::INCLUDE_ENTITY constant Include entity row languages when listing languages.
PluginBase::INCLUDE_NEGOTIATED constant Include negotiated languages when listing languages.
PluginBase::init public function Initialize the plugin. Overrides ViewsPluginInterface::init 6
PluginBase::listLanguages protected function Makes an array of languages, optionally including special languages.
PluginBase::pluginTitle public function Return the human readable name of the display. Overrides ViewsPluginInterface::pluginTitle
PluginBase::preRenderAddFieldsetMarkup public static function Moves form elements into fieldsets for presentation purposes. Overrides ViewsPluginInterface::preRenderAddFieldsetMarkup
PluginBase::preRenderFlattenData public static function Flattens the structure of form elements. Overrides ViewsPluginInterface::preRenderFlattenData
PluginBase::queryLanguageSubstitutions public static function Returns substitutions for Views queries for languages.
PluginBase::setOptionDefaults protected function Fills up the options of the plugin with defaults.
PluginBase::themeFunctions public function Provide a full list of possible theme templates used by this style. Overrides ViewsPluginInterface::themeFunctions 1
PluginBase::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks 6
PluginBase::unpackOptions public function Unpacks options over our existing defaults. Overrides ViewsPluginInterface::unpackOptions
PluginBase::usesOptions public function Returns the usesOptions property. Overrides ViewsPluginInterface::usesOptions 8
PluginBase::validate public function Validate that the plugin is correct and can be saved. Overrides ViewsPluginInterface::validate 6
PluginBase::viewsTokenReplace protected function Replaces Views' tokens in a given string. 1
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT constant Query string to indicate the site default language.
PluginBase::__construct public function Constructs a PluginBase object. 19
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
QueryPluginBase::$groupOperator protected property Controls how the WHERE and HAVING groups are put together.
QueryPluginBase::$limit protected property Stores the limit of items that should be requested in the query.
QueryPluginBase::$pager public property A pager plugin that should be provided by the display.
QueryPluginBase::addSignature public function Add a signature to the query, if such a thing is feasible. 1
QueryPluginBase::alter public function Let modules modify the query just prior to finalizing it. 1
QueryPluginBase::getAggregationInfo public function Get aggregation info for group by queries. 1
QueryPluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
QueryPluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 1
QueryPluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 1
QueryPluginBase::getDateField public function Returns a Unix timestamp to database native timestamp expression. 1
QueryPluginBase::getDateFormat public function Creates cross-database date formatting. 1
QueryPluginBase::getEntityTableInfo public function Returns an array of all tables from the query that map to an entity type.
QueryPluginBase::getLimit public function Returns the limit of the query.
QueryPluginBase::getTimezoneOffset public function Get the timezone offset in seconds.
QueryPluginBase::loadEntities public function Loads all entities contained in the passed-in $results. 1
QueryPluginBase::query public function Generate a query and a countquery from all of the information supplied. Overrides PluginBase::query 1
QueryPluginBase::setGroupOperator public function Control how all WHERE and HAVING groups are put together.
QueryPluginBase::setLimit public function Set a LIMIT on the query, specifying a maximum number of results.
QueryPluginBase::setOffset public function Set an OFFSET on the query, specifying a number of results to skip.
QueryPluginBase::setupTimezone public function Set the database to the current user timezone. 1
QueryPluginBase::setWhereGroup public function Create a new grouping for the WHERE or HAVING clause.
QueryPluginBase::submitOptionsForm public function Handle any special handling on the validate form. Overrides PluginBase::submitOptionsForm 1
QueryPluginBase::summaryTitle public function Returns the summary of the settings in the display. Overrides PluginBase::summaryTitle
QueryPluginBase::validateOptionsForm public function Validate the options form. Overrides PluginBase::validateOptionsForm
QueryTest::$allItems protected property
QueryTest::$conditions protected property
QueryTest::$fields protected property
QueryTest::$orderBy protected property
QueryTest::addField public function
QueryTest::addOrderBy public function
QueryTest::addWhere public function
QueryTest::build public function Implements Drupal\views\Plugin\views\query\QueryPluginBase::build(). Overrides QueryPluginBase::build
QueryTest::buildOptionsForm public function Provide a form to edit options for this plugin. Overrides PluginBase::buildOptionsForm
QueryTest::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides QueryPluginBase::calculateDependencies
QueryTest::defineOptions protected function Information about options for all kinds of purposes will be held here. Overrides PluginBase::defineOptions
QueryTest::ensureTable public function
QueryTest::execute public function Executes query and fills associated view object with according values. Overrides QueryPluginBase::execute
QueryTest::match public function Check a single condition for a single element.
QueryTest::setAllItems public function Sets the allItems property.
QueryTest::setFieldTimezoneOffset public function Applies a timezone offset to the given field. Overrides QueryPluginBase::setFieldTimezoneOffset
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.