Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file and chart test #99

Open
wants to merge 2 commits into
base: 2.x-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img src="https://github.com/govCMS/dvf/wiki/images/dvf-logo.png" width="460"/>

The Data Visualisation Framework (DVF for short) is a Drupal 8/9 module that
The Data Visualisation Framework (DVF for short) is a Drupal 8/9/10 module that
allows you to turn boring data sources (Eg CSV or JSON file) into interactive
visualisation. This allows content authors to provide more meaning to raw data,
illustrate trends and engage users.
Expand Down
14 changes: 14 additions & 0 deletions dvf_ckan/src/Plugin/Visualisation/Source/CkanResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ public static function create(
* {@inheritdoc}
*/
public function getFields() {
// Generate a cache key for fields.
$cache_key = $this->getCacheKey('fields');
$cache_object = $this->cache->get($cache_key);

// Use cache data if it exists, otherwise fetch fields and store it in cache.
if (is_object($cache_object)) {
$raw_fields = $cache_object->data;
}
Expand All @@ -192,13 +194,16 @@ public function getFields() {
* An array of CKAN resource fields.
*/
protected function fetchFields() {
// Construct a CKAN API query to retrieve fields for the specified resource ID.
$query = [
'id' => $this->getResourceId(),
'limit' => 1,
];

// Merge additional data filters into the CKAN API query.
$query = array_merge($query, $this->getDataFilters());

// Use the CKAN client to make a request to 'action/datastore_search'.
try {
$response = $this->ckanClient->get('action/datastore_search', $query);
$result = ($response->success === TRUE) ? $response->result : NULL;
Expand All @@ -208,6 +213,7 @@ protected function fetchFields() {
$result = NULL;
}

// Return the fields obtained from the CKAN datastore or an empty array if there are issues.
return ($result) ? $result->fields : [];
}

Expand Down Expand Up @@ -262,6 +268,7 @@ protected function fetchRecords(array $records = [], $limit = 100, $offset = 0)

$query = array_merge($query, $this->getDataFilters());

// Try to fetch records from the CKAN datastore using the CKAN client.
try {
$response = $this->ckanClient->get('action/datastore_search', $query);
$result = ($response->success === TRUE) ? $response->result : NULL;
Expand All @@ -271,10 +278,13 @@ protected function fetchRecords(array $records = [], $limit = 100, $offset = 0)
$result = NULL;
}

// Update records, total, and offset based on the fetched result.
$records = ($result) ? array_merge($records, $result->records) : [];
$total = ($result) ? $result->total : 0;
$offset = count($records);

// If there are more records to fetch (total is greater than the current offset),
// recursively call fetchRecords with updated parameters.
if ($total > $offset) {
return $this->fetchRecords($records, $limit, $offset);
}
Expand Down Expand Up @@ -316,15 +326,19 @@ protected function getCacheKey($object_type) {
* An array of data filters.
*/
protected function getDataFilters() {
// Retrieve configuration options for the visualisation's style.
$configuration_options = $this->visualisation->getConfiguration('style');
$data_filters = [];

// We bail early if data filters are not specified in the visualisation configuration.
if (empty($configuration_options['style']['options']['data']['data_filters'])) {
return $data_filters;
}

// Extract data filters from the visualisation configuration.
$data_filters = $configuration_options['style']['options']['data']['data_filters'];

// Check and validate if 'filters' key exists and contains valid JSON.
if (empty($data_filters['filters']) || !$this->dvfHelpers->validateJson($data_filters['filters'])) {
unset($data_filters['filters']);
}
Expand Down
7 changes: 7 additions & 0 deletions dvf_json/src/Plugin/Visualisation/Source/JsonFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function getFields() {
// Retrieves the fields available in the data provided by the source.
$fields = [];
$data = $this->getData();

Expand All @@ -184,6 +185,7 @@ public function getFields() {
public function getRecords() {
$records = [];

// Retrieves and structures records based on the configured JSON expression.
try {
$json = new JSONPath($this->getData());
$json = $json->find($this->config('json', 'expression'));
Expand All @@ -195,6 +197,9 @@ public function getRecords() {
return $records;
}

// Loop through each field defined by getFields(), associating the field
// labels with their corresponding values in the parsed JSON. The resulting
// records are stored in the $records array, organized by record ID.
foreach ($this->getFields() as $field_key => $field_label) {
foreach ($json->getData() as $record_id => $record) {
if (!isset($records[$record_id])) {
Expand All @@ -217,6 +222,7 @@ public function getData() {
$cache_key = $this->getCacheKey();
$cache_object = $this->cache->get($cache_key);

// Retrieves data, either from cache or by fetching it and caching it.
if (is_object($cache_object)) {
$data = $cache_object->data;
}
Expand All @@ -230,6 +236,7 @@ public function getData() {

/**
* Fetches the JSON data.
* It also validates the retrieved data returns NULL if there any errors.
*
* @return string
* A string containing the JSON file data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public function settingsSummary() {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
// Processes each item's visualisation style options, and merges them with the
// formatter settings. The resulting configuration is used to render the
// visualisation plugin for each item.
$element = [];
$formatter_settings = $this->getSettings();

Expand Down
11 changes: 11 additions & 0 deletions src/Plugin/Visualisation/Style/PieChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\dvf\Plugin\Visualisation\Style;

use Drupal\Core\Form\FormStateInterface;
use Drupal\dvf\FormElementAttributesTrait;

/**
Expand All @@ -25,6 +26,16 @@ public function defaultConfiguration() {
] + parent::defaultConfiguration();
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['chart']['#title'] = $this->t('Pie chart settings');

return $form;
}

/**
* {@inheritdoc}
*/
Expand Down
77 changes: 66 additions & 11 deletions tests/src/Functional/DvfFieldBasicConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,51 @@
class DvfFieldBasicConfigTest extends DvfFieldTestBase {

/**
* Tests the configuration of a visualisation file with csv source.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Drupal\Core\Entity\EntityStorageException
* Tests the configuration of a visualisation files.
*/
public function testConfigureFieldVisualisationFileCsv() {
public function testConfigureFieldVisualisationFiles() {
$fileTypes = [
'csv',
'json',
];

foreach ($fileTypes as $fileType) {
$this->configureFieldVisualisationFile($fileType);
}
}

/**
* Test that the visualisation style config properties are correct.
*/
public function testVisualisationStyleProperties() {
$visualisation_styles = [
'bar_chart',
'bubble_chart',
'donut_chart',
'gauge_chart',
'line_chart',
'pie_chart',
'radar_chart',
'scatter_plot_chart',
'spline_chart',
'table',
];

foreach ($visualisation_styles as $visualisation_style) {
$settingsText = ucfirst(str_replace('_', ' ', $visualisation_style)) . ' settings';
$this->configureVisualisationStyle('csv', "dvf_$visualisation_style", $settingsText);
}
}

/**
* Configure the visualisation style dropdown properties.
*/
public function configureVisualisationStyle($fileType = 'csv', $style = 'dvf_bar_chart', $settingsText = '') {
$assert_session = $this->assertSession();
// Create a new file field csv and attach to page bundle.
$field_name = 'testing_field_vis_file_csv';
// Create a new file field json and attach to page bundle.
$field_name = "field_{$fileType}_{$style}";
$field_settings = [
'visualisation_source' => 'dvf_csv_file',
'visualisation_source' => "dvf_{$fileType}_file",
];
$this->createDvfFileField($field_name, $field_settings);

Expand All @@ -42,17 +75,39 @@ public function testConfigureFieldVisualisationFileCsv() {
$this->visitAndAssertText($edit_page_path, $field_name);

// Upload a csv sample file to node page.
$csv_sample_filename = 'fruits.csv';
$csv_sample_filename = "fruits.$fileType";
$this->uploadSampleFileToNode($csv_sample_filename, $field_name);

// Confirm that file name is displayed on page.
$assert_session->pageTextNotContains('could not be uploaded');

// Select a visualisation style.
$assert_session->fieldExists($field_name . '[0][options][visualisation_style]')
->setValue($this->defaultVisualisationStyle);
->setValue($style);
$this->submitForm([], $this->t('Save'));
$this->drupalGet($edit_page_path);
$assert_session->pageTextContains($settingsText);

return [
'field_name' => $field_name,
'assert_session' => $assert_session,
'nid' => $nid,
];
}

/**
* Common test for the configuration of a visualisation file.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function configureFieldVisualisationFile($fileType = 'csv') {
$response = $this->configureVisualisationStyle($fileType,'dvf_bar_chart', 'Bar chart settings');

$field_name = $response['field_name'];
$nid = $response['nid'];
$assert_session = $response['assert_session'];

// Select visualisation style options.
// @codingStandardsIgnoreStart - ignore line exceed warning.
Expand Down