Skip to content
Snippets Groups Projects
Commit e2cb03f0 authored by Nia Kathoni's avatar Nia Kathoni Committed by Daniel Cothran
Browse files

Issue #3489680: Code updates

parent 27e9a2bb
No related branches found
No related tags found
1 merge request!10Code updates
Checking pipeline status
......@@ -2,6 +2,6 @@ name: Charts Highcharts Drilldown
description: 'Use this module to build a Highcharts drilldown chart.'
type: module
package: Charts
core_version_requirement: ^8.7.7 || ^9 || ^10 || ^11
core_version_requirement: ^9 || ^10 || ^11
dependencies:
- charts:charts
- charts:charts_highcharts
<?php
/**
* @file
* File implementing hooks related to the charts_highcharts_drilldown.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
......@@ -26,44 +31,32 @@ function charts_highcharts_drilldown_chart_alter(array &$element, $chart_id) {
$element['#attached']['library'][] = 'charts_highcharts_drilldown/drilldown';
}
/**
* Implements hook_chart_definition_alter().
*/
function charts_highcharts_drilldown_chart_definition_alter(array &$definition, array $element, $chart_id) {
if ($element['#chart_library'] === 'highcharts' && isset($definition['drilldown'])) {
$yaxis_title = $definition['yAxis'][0]['title']['text'];
$plotLines = [];
if (isset($definition['yAxis']['plotLines'])) {
$plotLines = $definition['yAxis']['plotLines'];
}
unset($definition['yAxis']);
$definition['yAxis']['title']['text'] = $yaxis_title;
if ($plotLines != NULL) {
$definition['yAxis']['plotLines'] = $plotLines;
}
unset($definition['xAxis']);
$definition['xAxis']['type'] = 'category';
// Get drilldown series.
$name = t('Home');
foreach ($definition['series'] as $single) {
if (isset($single['data'][0]['drilldown'])) {
$drilldown = $single;
$drilldown['name'] = $name;
}
}
unset($definition['series']);
$definition['series'][] = $drilldown;
$definition['accessibility']['announceNewData'] = [
'enabled' => TRUE,
];
$definition['plotOptions']['series']['dataLabels'] = [
'enabled' => TRUE,
];
if (isset($definition['legend']) && $definition['legend']['enabled'] == TRUE && !in_array($definition["chart"]["type"], [
'donut',
'pie',
])) {
$definition['legend']['symbolPadding'] = 0;
$definition['legend']['symbolWidth'] = 0;
$definition['legend']['symbolHeight'] = 0;
$definition['legend']['squareSymbol'] = FALSE;
}
if ($element['#chart_library'] !== 'highcharts' || !isset($definition['drilldown'])) {
return;
}
// Overwriting yAxis.
$yaxis = [
'title' => ['text' => $definition['yAxis'][0]['title']['text'] ?? ''],
];
if (!empty($definition['yAxis']['plotLines'])) {
$yaxis['plotLines'] = $definition['yAxis']['plotLines'];
}
$definition['yAxis'] = $yaxis;
// Overwriting xAxis.
$definition['xAxis'] = ['type' => 'category'];
$definition['accessibility']['announceNewData'] = ['enabled' => TRUE];
$definition['plotOptions']['series']['dataLabels'] = ['enabled' => TRUE];
if (!empty($definition['legend']['enabled']) && !in_array($definition['chart']['type'], ['donut', 'pie'])) {
$definition['legend']['symbolPadding'] = 0;
$definition['legend']['symbolWidth'] = 0;
$definition['legend']['symbolHeight'] = 0;
$definition['legend']['squareSymbol'] = FALSE;
}
}
views.style.charts_highcharts_drilldown:
type: views_style
views.style.chart_highcharts_drilldown:
type: views.style.chart
label: 'Chart highcharts drilldown'
mapping:
chart_settings:
type: charts_config
label: 'Settings'
fields_series_field:
type: string
label: 'Series Field'
......@@ -17,4 +14,3 @@ views.style.charts_highcharts_drilldown:
fields_operator:
type: string
label: 'Operator Field'
......@@ -4,7 +4,8 @@ namespace Drupal\charts_highcharts_drilldown\Plugin\views\style;
use Drupal\charts\Plugin\views\style\ChartsPluginStyleChart;
use Drupal\Component\Utility\Html;
use Drupal\core\form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Style plugin to render view as a chart.
......@@ -21,6 +22,19 @@ use Drupal\core\form\FormStateInterface;
*/
class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
/**
* List of the supported chart types for the drilldown functionality.
*
* @var array|string[]
*/
protected array $supportedChartTypes = [
'bar',
'column',
'donut',
'pie',
];
/**
* {@inheritdoc}
*/
......@@ -30,7 +44,7 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
$options['fields_series_field'] = ['default' => ''];
$options['fields_drilldown_series_field'] = ['default' => ''];
$options['fields_data_field'] = ['default' => ''];
$options['fields_operator'] = ['default' => ''];
$options['fields_operator'] = ['default' => 'sum'];
return $options;
}
......@@ -42,7 +56,7 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
parent::buildOptionsForm($form, $form_state);
$data_options = $this->displayHandler->getFieldLabels();
// reduce chart type options
// Reduce chart type options.
unset($form['grouping']);
$form['fields_series_field'] = [
'#type' => 'select',
......@@ -51,6 +65,7 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
'#weight' => -6,
'#default_value' => $this->options['fields_series_field'],
'#description' => $this->t('Select the series field.'),
'#required' => TRUE,
];
$form['fields_drilldown_series_field'] = [
'#type' => 'select',
......@@ -59,6 +74,7 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
'#weight' => -5,
'#default_value' => $this->options['fields_drilldown_series_field'],
'#description' => $this->t('Select the drilldown field.'),
'#required' => TRUE,
];
$form['fields_data_field'] = [
'#type' => 'select',
......@@ -67,6 +83,7 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
'#weight' => -4,
'#default_value' => $this->options['fields_data_field'],
'#description' => $this->t('Select the data field (this data will be aggregated for the parent chart).'),
'#required' => TRUE,
];
$form['fields_operator'] = [
'#type' => 'radios',
......@@ -77,178 +94,186 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
'#description' => $this->t('Select the operator for the aggregation method.'),
];
$form_state->set('default_options', $this->options);
}
/**
* {@inheritdoc}
*/
public function render() {
$chart = parent::render();
$form_options = $this->options;
$chart_settings = $this->options['chart_settings'];
// If the chart is bar, column or pie only.
if ($chart_settings['type'] == 'bar' || $chart_settings['type'] == 'column' || $chart_settings['type'] == 'pie') {
if ($form_options['fields_series_field'] != NULL && $form_options['fields_drilldown_series_field'] != NULL &&
$form_options['fields_data_field'] != NULL && $form_options['fields_operator'] != NULL) {
$parent_field = $form_options['fields_series_field'];
$drilldown_field = $form_options['fields_drilldown_series_field'];
$data_field = $form_options['fields_data_field'];
$operator = $form_options['fields_operator'];
$selected_data_fields = is_array($chart_settings['fields']['data_providers']) ? $this->getSelectedDataFields($chart_settings['fields']['data_providers']) : NULL;
$selected_data_fields = array_keys($selected_data_fields);
$chart['#id'] = Html::getId($this->view->id() . '_' . $this->view->current_display);
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
parent::validateOptionsForm($form, $form_state);
$this->renderFields($this->view->result);
$renders = $this->rendered_fields;
$chart_settings = $form_state->getValue([
'style_options',
'chart_settings',
]);
if (!in_array($chart_settings['type'], $this->supportedChartTypes)) {
$form_state->setError($form['chart_settings'], $this->t('The Chart highcharts drilldown views style does not support the selected @chart chart type. Only the following chart types are supported: @chart_types.', [
'@chart_types' => implode(', ', $this->supportedChartTypes),
]));
}
}
$parents = [];
$global_data = [];
$series = [];
$drilldown = [];
/**
* {@inheritdoc}
*/
public function validate() {
$errors = parent::validate();
if (in_array($data_field, $selected_data_fields)) {
// Build global array: Parent, child and values.
foreach ($renders as $row_number => $row) {
$data_row = [];
$data_row[] = $this->processNumberValueFromField($row_number, $parent_field);
$data_row[] = $this->processNumberValueFromField($row_number, $drilldown_field);
$data_row[] = $this->processNumberValueFromField($row_number, $data_field);
$global_data[] = $data_row;
}
// Get a list of parent names.
foreach ($global_data as $row) {
$parents[] = $row[0];
}
$parents = array_unique($parents);
// Build drilldown series
foreach ($parents as $parent) {
$child = ['name' => $parent, 'id' => $parent];
foreach ($global_data as $row) {
if ($parent == $row[0]) {
$child['data'][] = [$row[1], $row[2] + 0];
}
}
$drilldown['series'][] = $child;
}
// Build Parent series
$k = 0;
$series_object['name'] = $this->view->field[$parent_field]->options['label'];
$precision = 0;
if ($chart_settings['yaxis']['decimal_count'] != "") {
$precision = $chart_settings['yaxis']['decimal_count'];
}
if ($operator == 'sum') {
foreach ($parents as $parent) {
$val = 0;
foreach ($global_data as $row) {
if ($parent == $row[0]) {
$val = $val + $row[2];
}
}
$val = $val + 0;
$k = $this->getColorIndex($k);
$child_2 = [
'name' => $parent,
'y' => $val,
'drilldown' => $parent,
'color' => $chart_settings['display']['colors'][$k],
];
$series_object['data'][] = $child_2;
$k++;
}
}
elseif ($operator == 'average') {
foreach ($parents as $parent) {
$numerator = 0;
$denominator = 0;
foreach ($global_data as $row) {
if ($parent == $row[0]) {
$numerator = $numerator + $row[2];
$denominator++;
}
}
$val = $denominator != 0 ? round($numerator / $denominator, $precision) : 0;
$k = $this->getColorIndex($k);
$child_2 = [
'name' => $parent,
'y' => $val,
'drilldown' => $parent,
'color' => $chart_settings['display']['colors'][$k],
];
$series_object['data'][] = $child_2;
$k++;
}
}
$series[] = $series_object;
$options['title']['text'] = $chart_settings['display']['title'];
$options['series'] = $series;
$options['drilldown'] = $drilldown;
$chart['#raw_options'] = $options;
}
else {
\Drupal::logger('Charts_highcharts_drilldown')
->notice('Select data field in providers fields.');
}
}
else {
\Drupal::logger('Charts_highcharts_drilldown')
->notice('Select parent, drilldown, data and operator fields in the option form.');
$chart_settings = $this->options['chart_settings'];
if (!in_array($chart_settings['type'], $this->supportedChartTypes)) {
$errors[] = $this->t('The Chart highcharts drilldown views style does not support the selected @chart chart type. Only the following chart types are supported: @chart_types.', [
'@chart_types' => implode(', ', $this->supportedChartTypes),
]);
}
$option_labels = [
'fields_series_field' => $this->t('Series Field'),
'fields_drilldown_series_field' => $this->t('Drilldown Field'),
'fields_data_field' => $this->t('Data Field'),
'fields_operator' => $this->t('Operator'),
];
foreach ($option_labels as $key => $label) {
if (empty($this->options[$key])) {
$errors[] = $this->t('The option "@key" is required by Chart highcharts drilldown views style to render the chart.', ['@key' => $label]);
}
}
else {
\Drupal::logger('Charts_highcharts_drilldown')
->notice('The chart should be either bar, column or pie.');
if (!$errors && !empty($this->options['fields_data_field'])) {
$data_field = $this->options['fields_data_field'];
$selected_data_fields = array_keys($this->getSelectedDataFields($chart_settings['fields']['data_providers'] ?? []));
$field_options = $this->displayHandler->getFieldLabels();
if (!in_array($data_field, $selected_data_fields)) {
$errors[] = $this->t('Please ensure that @field (@key) is also selected as data provider in the chart settings.', [
'@field' => $field_options[$data_field],
'@key' => $data_field,
]);
}
}
return $chart;
return $errors;
}
/**
* Get an index for config color
*
* {@inheritdoc}
*/
public function render() {
$chart_settings = $this->options['chart_settings'];
if (!in_array($chart_settings['type'], $this->supportedChartTypes)) {
throw new \LogicException(sprintf('The provided chart type "%s" is not supported by the Chart highcharts drilldown views style. Only the following chart types are supported: "%s"', $chart_settings['type'], implode(', ', $this->supportedChartTypes)));
}
$parent_field = $this->options['fields_series_field'];
$drilldown_field = $this->options['fields_drilldown_series_field'];
$operator = $this->options['fields_operator'];
$data_field = $this->options['fields_data_field'];
$selected_data_fields = array_keys($this->getSelectedDataFields($chart_settings['fields']['data_providers']));
if (!in_array($this->options['fields_data_field'], $selected_data_fields)) {
$field_options = $this->displayHandler->getFieldLabels();
throw new \LogicException(sprintf('Please ensure that %s (%s) is also selected as data provider in the chart settings.', $field_options[$data_field], $data_field));
}
$chart = parent::render();
// The chart display's title settings added to raw options.
$chart['#raw_options']['title']['text'] = $chart_settings['display']['title'];
public static function getColorIndex($k) {
if ($k < 10) {
return $k;
// Skip rendering fields in case parent chart style have already done it.
if (!isset($this->rendered_fields)) {
$this->renderFields($this->view->result);
}
else {
while ($k >= 10) {
$array = str_split($k);
$k = array_sum($array);
$renders = $this->rendered_fields;
$drilldown_series = [];
$main_chart_series = [];
$series_color_index = 0;
$display_colors_count = count($chart_settings['display']['colors']);
// Build drilldown series.
foreach ($renders as $row_number => $row) {
$drilldown_name = $this->processNumberValueFromField($row_number, $parent_field);
$drilldown_id = Html::getId($drilldown_name);
$drilldown_value = $this->processNumberValueFromField($row_number, $data_field) + 0;
// Instantiate drilldown data if not done yet.
if (empty($drilldown_data[$drilldown_id])) {
$drilldown_data[$drilldown_id] = [];
}
// Instantiate drilldown series variable if not done yet.
if (empty($drilldown_series[$drilldown_id])) {
$drilldown_series[$drilldown_id] = [
'name' => $drilldown_name,
'id' => $drilldown_id,
'data' => [],
];
}
return $k;
// Instantiate drilldown series variable if not done yet.
if (empty($main_chart_series[$drilldown_id])) {
$main_chart_series[$drilldown_id] = [
'name' => $drilldown_name,
'y' => 0,
'drilldown' => $drilldown_id,
'color' => $chart_settings['display']['colors'][$series_color_index],
];
$series_color_index++;
// Reset the color index to start from zero to be able to pick again
// from the assigned colors.
$series_color_index = $series_color_index === $display_colors_count ? 0 : $series_color_index;
}
$drilldown_data[$drilldown_id][] = $drilldown_value;
$drilldown_series[$drilldown_id]['data'][] = [
$this->processNumberValueFromField($row_number, $drilldown_field),
$drilldown_value,
];
$main_chart_series[$drilldown_id]['y'] = $this->calculateMainSeriesYaxisValue($operator, $drilldown_data[$drilldown_id]);
}
// Adding drilldown related settings under raw options.
$chart['#raw_options']['drilldown']['series'] = array_values($drilldown_series);
// Build the main chart series.
$chart['#raw_options']['series'] = [
[
'name' => !empty($this->view->field[$parent_field]->options['label']) ? $this->view->field[$parent_field]->options['label'] : t('Series'),
'data' => array_values($main_chart_series),
],
];
// Ensure that the series shows in the header and the point is associated with the color.
$chart['#raw_options']['tooltip']['headerFormat'] = '<span style="font-size: 0.9em">{series.name}</span><br>';
$chart['#raw_options']['tooltip']['pointFormat'] = '<span style="color:{point.color}">●</span> <span>{point.name}</span>: <b>{point.y}</b>';
// Remove chart_data so that highcharts doesn't end up adding them as
// the main chart series.
foreach (Element::children($chart) as $key) {
$children_element_type = $chart[$key]['#type'] ?? '';
if ($children_element_type === 'chart_data' && empty($chart[$key]['#chart_attachment_id'])) {
unset($chart[$key]);
}
}
return $chart;
}
/**
* Utility method to filter out unselected fields from data providers fields.
* Calculates the main series Yaxis value.
*
* @param array $data_providers
* The data providers.
* @param string $aggregation_function
* The drilldown aggregation function.
* @param array $drilldown_data
* The drilldown data collected so far.
*
* @return array
* The fields.
* @return int|float
* The calculated value.
*/
private function getSelectedDataFields(array $data_providers) {
return array_filter($data_providers, function ($value) {
return $this->checkFieldEnabled($value);
});
}
public static function checkFieldEnabled($value) {
if (gettype($value) != 'array') {
return FALSE;
protected function calculateMainSeriesYaxisValue(string $aggregation_function, array $drilldown_data): int|float {
if (!in_array($aggregation_function, ['sum', 'avg', 'average'])) {
return 0;
}
if (!isset($value['enabled'])) {
return FALSE;
$value = array_sum($drilldown_data);
if ($aggregation_function !== 'sum') {
$chart_settings = $this->options['chart_settings'];
$precision = !empty($chart_settings['yaxis']['decimal_count']) ? $chart_settings['yaxis']['decimal_count'] : 0;
$denominator = count($drilldown_data);
$value = $denominator > 0 ? round($value / $denominator, $precision) : 0;
}
return !empty($value['enabled']);
return $value;
}
/**
......@@ -263,11 +288,10 @@ class ChartsDrilldownPluginStyleChart extends ChartsPluginStyleChart {
* The value.
*/
public function processNumberValueFromField($number, $field) {
$value = trim(strip_tags((string)$this->getField($number, $field)));
$value = trim(strip_tags((string) $this->getField($number, $field)));
if ($value === '' || is_null($value)) {
$value = NULL;
return NULL;
}
return $value;
}
......
name: 'Charts Highcharts Drilldown Test'
type: module
description: 'Provides functionality for testing the charts_highcharts_drilldown module.'
package: Testing
core_version_requirement: ^9 || ^10 || ^11
dependencies:
- charts_highcharts_drilldown:charts_highcharts_drilldown
langcode: en
status: true
dependencies: { }
id: test_charts_highcharts_drilldown
label: ''
module: views
description: ''
tag: ''
base_table: views_test_data
base_field: nid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: 'Test charts highcharts drilldown style'
defaults:
fields: false
pager: false
sorts: false
fields:
age:
id: age
field: age
relationship: none
table: views_test_data
plugin_id: numeric
label: 'Age'
job:
id: job
field: job
relationship: none
table: views_test_data
plugin_id: string
label: 'Job'
age_1:
id: age_1
field: age
relationship: none
table: views_test_data
plugin_id: string
label: 'Age type'
alter:
alter_text: true
text: "{% if age_1 < 30 %}\r\nBelow 30\r\n{% else %}\r\nOver 30\r\n{% endif %}"
pager:
type: none
options:
offset: 0
empty:
area_text_custom:
id: area_text_custom
table: views
field: area_text_custom
relationship: none
group_type: group
admin_label: ''
plugin_id: text_custom
empty: true
content: 'no charts visible for the drilldown style'
tokenize: false
sorts:
id:
field: id
id: id
order: ASC
relationship: none
table: views_test_data
plugin_id: numeric
style:
type: chart_highcharts_drilldown
options:
fields_series_field: age_1
fields_drilldown_series_field: job
fields_data_field: age
fields_operator: sum
chart_settings:
library: highcharts
type: column
fields:
label: ''
stacking: false
data_providers:
age:
enabled: true
color: '#ff9300'
weight: 4
job:
enabled: false
color: '#d783ff'
weight: 4
age_1:
enabled: false
color: '#b76b1f'
weight: 4
display:
title: 'Test charts highcharts drilldown'
title_position: out
subtitle: ''
data_labels: true
data_markers: true
legend_position: bottom
background: ''
three_dimensional: 0
polar: 0
tooltips: true
dimensions:
width: ''
width_units: ''
height: ''
height_units: ''
gauge:
max: ''
min: ''
green_from: ''
green_to: ''
yellow_from: ''
yellow_to: ''
red_from: ''
red_to: ''
color_changer: false
xaxis:
title: Date
labels_rotation: '0'
yaxis:
title: Downloads
min: ''
max: ''
prefix: ''
suffix: ''
decimal_count: ''
labels_rotation: '0'
row:
type: fields
page_1:
id: page_1
display_title: 'Page display'
display_plugin: page
position: 1
display_options:
path: test-charts-highcharts-drilldown
<?php
namespace Drupal\Tests\charts_highcharts_drilldown\Functional\Plugin;
use Drupal\Component\Utility\Html;
use Drupal\Tests\views\Functional\ViewTestBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Tests the chart_highcharts_drilldown style views plugin.
*
* @group charts_highcharts_drilldown
*/
class StyleChartsHighchartsDrilldownTest extends ViewTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_charts_highcharts_drilldown'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'charts',
'charts_highcharts',
'charts_highcharts_drilldown',
'charts_highcharts_drilldown_test',
'views',
];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = ['charts_highcharts_drilldown_test']): void {
parent::setUp($import_test_views, $modules);
$this->enableViewsTestModule();
}
/**
* Tests the generated JSON generated from the views.
*/
public function testGeneratedJson() {
$this->drupalGet('test-charts-highcharts-drilldown');
$this->assertSession()->statusCodeEquals(Response::HTTP_OK);
$attribute = 'data-chart';
$display_id = 'page_1';
$view_id_selector = Html::getId('chart-' . static::$testViews[0] . '-' . $display_id);
$chart_container = $this->assertSession()
->elementAttributeExists('css', "#{$view_id_selector}", $attribute);
$actual = str_replace('&quot;', '"', (string) $chart_container->getAttribute($attribute));
$expected = '{"chart":{"type":"column","backgroundColor":"","polar":0,"options3d":{"enabled":0}},"credits":{"enabled":false},"title":{"text":"Test charts highcharts drilldown","style":{"color":"#000"}},"subtitle":{"text":""},"colors":["#006fb0","#f07c33","#342e9c","#579b17","#3f067a","#cbde67","#7643b6","#738d00","#c157c7","#02dab1","#ed56b4","#d8d981","#004695","#736000","#a5a5ff","#833a00","#ff9ee9","#684507","#fe4f85","#5d0011","#ffa67b","#88005c","#ff9b8f","#85000f","#ff7581"],"tooltip":{"enabled":true,"useHTML":false,"headerFormat":"\u003Cspan style=\u0022font-size: 0.9em\u0022\u003E{series.name}\u003C\/span\u003E\u003Cbr\u003E","pointFormat":"\u003Cspan style=\u0022color:{point.color}\u0022\u003E\u25cf\u003C\/span\u003E \u003Cspan\u003E{point.name}\u003C\/span\u003E: \u003Cb\u003E{point.y}\u003C\/b\u003E"},"plotOptions":{"series":{"stacking":false,"dataLabels":{"enabled":true},"marker":{"enabled":true}}},"legend":{"enabled":true,"verticalAlign":"bottom","layout":"horizontal","symbolPadding":0,"symbolWidth":0,"symbolHeight":0,"squareSymbol":false},"drilldown":{"series":[{"name":"Below 30","id":"below-30","data":[["Singer",25],["Singer",27],["Drummer",28],["Songwriter",26]]},{"name":"Over 30","id":"over-30","data":[["Speaker",30]]}]},"xAxis":{"type":"category"},"yAxis":{"title":{"text":"Downloads"}},"series":[{"name":"Age type","data":[{"name":"Below 30","y":106,"drilldown":"below-30","color":"#006fb0"},{"name":"Over 30","y":30,"drilldown":"over-30","color":"#f07c33"}]}],"accessibility":{"announceNewData":{"enabled":true}}}';
$this->assertEquals($expected, $actual);
}
}
<?php
namespace Drupal\Tests\charts_highcharts_drilldown\Unit\Plugin;
use Drupal\Tests\UnitTestCase;
use Drupal\charts_highcharts_drilldown\Plugin\views\style\ChartsDrilldownPluginStyleChart;
/**
* @coversDefaultClass \Drupal\charts_highcharts_drilldown\Plugin\views\style\ChartsDrilldownPluginStyleChart
* @group charts_highcharts_drilldown
*/
class DrilldownPluginTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
}
/**
* Test the colorIndexUnderTen() method.
*
* @requires module charts
*/
public function testColorIndexUnderTen() {
$this->assertEquals(1, ChartsDrilldownPluginStyleChart::getColorIndex(1));
$this->assertEquals(8, ChartsDrilldownPluginStyleChart::getColorIndex(8));
}
/**
* Test the colorIndexUpTen() method.
*
* @requires module charts
*/
public function testColorIndexUpTen() {
$this->assertEquals(1, ChartsDrilldownPluginStyleChart::getColorIndex(10));
$this->assertEquals(6, ChartsDrilldownPluginStyleChart::getColorIndex(15));
$this->assertEquals(5, ChartsDrilldownPluginStyleChart::getColorIndex(59));
}
/**
* Test the fieldDisabled() method.
*
* @requires module charts
*/
public function testFieldDisabled() {
$input = [
"enabled" => FALSE,
"color" => "#000000",
"weight" => 121,
];
$this->assertEquals(FALSE, ChartsDrilldownPluginStyleChart::checkFieldEnabled($input));
}
/**
* Test the fieldEnabled() method.
*
* @requires module charts
*/
public function testFieldEnabled() {
$input = [
"enabled" => TRUE,
"color" => "#000000",
"weight" => 121,
];
$this->assertEquals(TRUE, ChartsDrilldownPluginStyleChart::checkFieldEnabled($input));
}
/**
* Test the fieldMissignEnabled() method.
*
* @requires module charts
*/
public function testFieldMissingEnabled() {
$input = [
"color" => "#000000",
"weight" => 121,
];
$this->assertEquals(FALSE, ChartsDrilldownPluginStyleChart::checkFieldEnabled($input));
}
/**
* Test the fieldEnabledNoArray() method.
*
* @requires module charts
*/
public function testFieldEnabledNoArray() {
$this->assertEquals(FALSE, ChartsDrilldownPluginStyleChart::checkFieldEnabled(NULL));
$this->assertEquals(FALSE, ChartsDrilldownPluginStyleChart::checkFieldEnabled('STRING'));
}
}
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