Problem/Motivation

In order to grow the module, we need to add additional chart types (and some chart types currently supported are not functional for ALL libraries). We need a dynamic way to exclude chart type options when the selected library does not support those types.

Issue fork charts-3390738

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andileco created an issue. See original summary.

andileco’s picture

Status: Active » Needs review
StatusFileSize
new12.68 KB
andileco’s picture

StatusFileSize
new12.68 KB

Fixes two minor coding standards issues.

nikathone’s picture

Did a quick review of the code and some initial manual test. I think we need to create a follow up issue for adding test coverage or commit the current patch, then change its status to needs work and tag it with need tests.

andileco’s picture

Thank you, @nikathone! Let's commit this and then add tags for tests.

  • andileco committed 077d8527 on 5.0.x authored by nikathone
    Issue #3390738 by andileco, nikathone: Dynamically hide chart types that...
andileco’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Adding the needs tests tag.

diaodiallo’s picture

Status: Needs work » Needs review
StatusFileSize
new3.73 KB

Added test coverage as suggested.

diaodiallo’s picture

Fixed two coding standard issues.

nikathone’s picture

Status: Needs review » Needs work
+++ b/src/Element/Chart.php
@@ -202,12 +203,11 @@ class Chart extends RenderElement implements ContainerFactoryPluginInterface {
+      throw new PluginNotFoundException($plugin->getChartName(), 'Chart type @type not supported by @plugin', [
+        '@type' => $type_name,
+        '@plugin' => $plugin->getChartName(),
+      ]);

Maybe we should throw a logic exception here instead of the plugin not found one. This could be something like throw new \LogicException(sprintf('The provided chart type "%s" is not supported by "%s" chart plugin library.', $type_name, $plugin->getChartName()));.

If you agree with the above then we should also update the test to catch the logic exception.

+++ b/tests/src/Kernel/ChartTypeSupportTest.php
@@ -0,0 +1,58 @@
+    $this->expectException(PluginNotFoundException::class);
+    $this->assertNotEmpty($this->renderer->renderRoot($element));

I don't think we need to assert that the element is not empty since our primary goal is to check for the exception. However we need to change the expected exception to $this->expectException(\LogicException::class); and also maybe check for the expected message to see if it includes our "not_supported" type and the "charts_test_library" something like $this->expectExceptionMessage('The provided chart type "not_supported" is not supported by "Charts Test Library" chart plugin library.'); should do.

+++ b/tests/src/Kernel/ChartTypeSupportTest.php
@@ -0,0 +1,58 @@
+    $element = [
+      '#type' => 'chart',
+      '#library' => 'charts_test_library',
+      '#chart_type' => 'bar',
+    ];
+
+    // Assert that bar chart type is supported. The chart should be rendered.
+    $this->assertNotEmpty($this->renderer->renderRoot($element));
+

This part is already covered by https://git.drupalcode.org/project/charts/-/blob/5.0.x/tests/src/Kernel/.... If for example in that test they were using a not supported type the test would fail.

  • andileco committed 647c9785 on 5.0.x authored by nikathone
    Issue #3390738 by nikathone, andileco, diaodiallo: Dynamically hide...
andileco’s picture

Status: Needs work » Fixed

This works really well - thank you!

  • andileco committed a866ee16 on 5.0.x authored by nikathone
    Issue #3390738 by nikathone, andileco, diaodiallo: Dynamically hide...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.