Skip to content
7 changes: 7 additions & 0 deletions src/plugins/PluginContainer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ describe('PluginContainer', () => {
expect(getByText('Direct plugin')).toBeInTheDocument();
});

it('renders no plugin when passed an unrecognized type in the configuration', () => {
const { queryByText } = render(<PluginContainerWrapper type="unknown_plugin" />);

expect(queryByText('Iframe plugin')).not.toBeInTheDocument();
expect(queryByText('Direct plugin')).not.toBeInTheDocument();
});

describe('ErrorBoundary', () => {
beforeAll(() => {
const ExplodingComponent = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/PluginSlot.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('PluginSlot', () => {
});
render(<TestPluginSlot />);

expect(logError).toHaveBeenCalledWith('the insert operation config is invalid for widget id: invalid_config');
expect(logError).toHaveBeenCalledWith('the insert operation config has an unrecognized type for widget id: invalid_config');
});

it('should handle multiple children', () => {
Expand Down
37 changes: 25 additions & 12 deletions src/plugins/data/utils.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
import React from 'react';
import { getConfig } from '@edx/frontend-platform';
import { logError } from '@edx/frontend-platform/logging';
import { PLUGIN_OPERATIONS, requiredPluginTypes } from './constants';
import {
DIRECT_PLUGIN, IFRAME_PLUGIN, PLUGIN_OPERATIONS, requiredPluginTypes,
} from './constants';

/**
* Called by validatePlugin to compare plugin config to the required data and data types
* @returns {Boolean} - returns true if all types are correct and present according to the plugin operation
*/
const validateRequirements = (requiredTypes, widgetConfig) => Object.keys(requiredTypes).every(
// eslint-disable-next-line valid-typeof
(field) => (widgetConfig[field] && (typeof widgetConfig[field] === requiredTypes[field])),
(field) => typeof widgetConfig[field] === requiredTypes[field],
);

/**
* Called by organizePlugins to validate plugin configurations
* @returns {Boolean} - boolean if all types are correct and present, else throws an error
* @returns {Boolean} - true if all types are correct and present, otherwise logs an error and returns false
*/
export const validatePlugin = (pluginConfig) => {
let requiredTypes = {};
const { op } = pluginConfig;
const { op } = pluginConfig || {};
let config = pluginConfig;

if (!op) { logError('There is a config with an invalid PLUGIN_OPERATION. Check to make sure it is configured correctly.'); }
if (!Object.values(PLUGIN_OPERATIONS).includes(op)) {
logError('There is a config with an invalid PLUGIN_OPERATION. Check to make sure it is configured correctly.');
return false;
}

let requiredTypes = requiredPluginTypes[op];

if (op === PLUGIN_OPERATIONS.Insert) {
config = config.widget;
if (!config) { logError('insert operation config is missing widget object'); }
if (!config) {
logError('insert operation config is missing widget object');
return false;
}

if (![DIRECT_PLUGIN, IFRAME_PLUGIN].includes(config.type)) {
logError(`the insert operation config has an unrecognized type for widget id: ${config.id}`);
return false;
}

requiredTypes = {
...requiredPluginTypes[op].base,
...requiredPluginTypes[op][config.type?.toLowerCase()],
...requiredTypes.base,
...requiredTypes[config.type.toLowerCase()],
};
Comment on lines 44 to 47

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the [DIRECT_PLUGIN, IFRAME_PLUGIN].includes(config.type) check below moved above this spread, this could be a plain config.type.toLowerCase(), and a non-string type would get the "unrecognized type" message instead of the generic one.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 144e0b4. The type check now runs before the spread, so the spread uses a plain config.type.toLowerCase(), and a missing or non-string type gets the "unrecognized type" message. I updated the tests to match. I also removed my extra PluginSlot test, because the existing INVALID_TYPE test now covers the same path.

} else {
requiredTypes = requiredPluginTypes[op];
}

if (!validateRequirements(requiredTypes, config)) {
logError(`the ${op} operation config is invalid for widget id: ${config.widgetId || config.id || 'MISSING ID'}`);
Comment thread
arbrandes marked this conversation as resolved.
Resolved
return false;
}

return true;
Expand All @@ -52,7 +65,7 @@ export const validatePlugin = (pluginConfig) => {
export const organizePlugins = (defaultContents, plugins) => {
const newContents = [...defaultContents];
plugins.forEach(change => {
validatePlugin(change);
if (!validatePlugin(change)) { return; }
if (change.op === PLUGIN_OPERATIONS.Insert) {
newContents.push(change.widget);
} else if (change.op === PLUGIN_OPERATIONS.Hide) {
Expand Down
195 changes: 134 additions & 61 deletions src/plugins/data/utils.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,94 @@ describe('organizePlugins', () => {
expect(plugins[1].id).toBe('default_contents');
expect(plugins[2].id).toBe('login');
});

it('should insert a plugin with priority 0 first', () => {
const plugins = organizePlugins(mockDefaultContent, [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'zero_priority_plugin',
priority: 0,
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
]);
expect(plugins.map((w) => w.id)).toEqual(['zero_priority_plugin', 'default_contents']);
});

it('should not insert a plugin that is missing a required property', () => {
const plugins = organizePlugins(mockDefaultContent, [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'no_priority_plugin',
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
]);
expect(plugins.length).toEqual(1);
expect(plugins.find((w) => w.id === 'no_priority_plugin')).toBeUndefined();
expect(logError).toHaveBeenCalledWith('the insert operation config is invalid for widget id: no_priority_plugin');
});

it('should ignore operations targeting a plugin that failed validation', () => {
const plugins = organizePlugins(mockDefaultContent, [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'no_priority_plugin',
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
{
op: PLUGIN_OPERATIONS.Modify,
widgetId: 'no_priority_plugin',
fn: mockModifyWidget,
},
{
op: PLUGIN_OPERATIONS.Wrap,
widgetId: 'no_priority_plugin',
wrapper: makeMockElementWrapper(),
},
]);
expect(plugins.length).toEqual(1);
expect(plugins[0].id).toBe('default_contents');
});

it('should preserve the priority ordering of valid plugins when an insert is invalid', () => {
const plugins = organizePlugins(mockDefaultContent, [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'second_plugin',
priority: 30,
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'no_priority_plugin',
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'first_plugin',
priority: 10,
type: DIRECT_PLUGIN,
RenderWidget: mockRenderWidget,
},
},
]);
expect(plugins.map((w) => w.id)).toEqual(['first_plugin', 'second_plugin', 'default_contents']);
});
});
});

Expand Down Expand Up @@ -328,36 +416,34 @@ describe('validatePlugin', () => {
url: 'www.example_url.com',
},
};
// plugin type is not a string
const insertBrokenNonStringTypeConfig = {
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'new_plugin',
priority: 10,
type: 5,
RenderWidget: mockRenderWidget,
},
};

expect(validatePlugin(insertBrokenDirectConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('the insert operation config is invalid for widget id: MISSING ID');

expect(validatePlugin(insertBrokenDirectConfig2)).toBe(false);
expect(logError).toHaveBeenCalledWith('the insert operation config is invalid for widget id: new_plugin');

expect(validatePlugin(insertBrokenDirectConfig3)).toBe(false);
expect(logError).toHaveBeenCalledWith('insert operation config is missing widget object');

try {
validatePlugin(insertBrokenDirectConfig);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the insert configuration is invalid for widget id: MISSING ID');
}

try {
validatePlugin(insertBrokenDirectConfig2);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the insert configuration is invalid for widget id: new_plugin');
}

try {
validatePlugin(insertBrokenDirectConfig3);
} catch (error) {
expect(logError).toHaveBeenCalledWith('insert operation config is missing widget object');
}

try {
validatePlugin(insertBrokenIFrameConfig);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the insert configuration is invalid for widget id: new_iframe_plugin');
}

try {
validatePlugin(insertBrokenIFrameConfig2);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the insert configuration is invalid for widget id: new_iframe_plugin');
}
expect(validatePlugin(insertBrokenIFrameConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('the insert operation config is invalid for widget id: new_iframe_plugin');

expect(validatePlugin(insertBrokenIFrameConfig2)).toBe(false);
expect(logError).toHaveBeenCalledWith('the insert operation config has an unrecognized type for widget id: new_iframe_plugin');

expect(validatePlugin(insertBrokenNonStringTypeConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('the insert operation config has an unrecognized type for widget id: new_plugin');
});
});
describe('hide plugin configuration', () => {
Expand All @@ -373,11 +459,8 @@ describe('validatePlugin', () => {
op: PLUGIN_OPERATIONS.Hide,
};

try {
validatePlugin(invalidHideConfig);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the hide operation config is invalid for widget id: MISSING ID');
}
expect(validatePlugin(invalidHideConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('the hide operation config is invalid for widget id: MISSING ID');
});
});
describe('modify plugin configuration', () => {
Expand All @@ -399,16 +482,10 @@ describe('validatePlugin', () => {
fn: mockModifyWidget,
};

try {
validatePlugin(invalidModifyConfig1);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the modify operation config is invalid for widget id: random_plugin');
}
try {
validatePlugin(invalidModifyConfig2);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the modify operation config is invalid for widget id: MISSING ID');
}
expect(validatePlugin(invalidModifyConfig1)).toBe(false);
expect(logError).toHaveBeenCalledWith('the modify operation config is invalid for widget id: random_plugin');
expect(validatePlugin(invalidModifyConfig2)).toBe(false);
expect(logError).toHaveBeenCalledWith('the modify operation config is invalid for widget id: MISSING ID');
});
});
describe('wrap plugin configuration', () => {
Expand All @@ -430,30 +507,26 @@ describe('validatePlugin', () => {
wrapper: makeMockElementWrapper(),
};

try {
validatePlugin(invalidWrapConfig1);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the wrap operation config is invalid for widget id: random_plugin');
}
try {
validatePlugin(invalidWrapConfig2);
} catch (error) {
expect(logError).toHaveBeenCalledWith('the wrap operation config is invalid for widget id: MISSING ID');
}
expect(validatePlugin(invalidWrapConfig1)).toBe(false);
expect(logError).toHaveBeenCalledWith('the wrap operation config is invalid for widget id: random_plugin');
expect(validatePlugin(invalidWrapConfig2)).toBe(false);
expect(logError).toHaveBeenCalledWith('the wrap operation config is invalid for widget id: MISSING ID');
});
});
describe('an invalid plugin configuration', () => {
it('should raise an error for an operation that does not exist', () => {
it.each([undefined, 'destroy'])('should return false and raise an error for operation "%s"', (op) => {
const invalidPluginConfig = {
op: PLUGIN_OPERATIONS.Destroy,
op,
widgetId: 'drafts',
};

try {
validatePlugin(invalidPluginConfig);
} catch (error) {
expect(logError).toHaveBeenCalledWith('There is a config with an invalid PLUGIN_OPERATION. Check to make sure it is configured correctly.');
}
expect(validatePlugin(invalidPluginConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('There is a config with an invalid PLUGIN_OPERATION. Check to make sure it is configured correctly.');
});

it.each([undefined, null])('should return false and raise an error for a "%s" plugin config', (pluginConfig) => {
expect(validatePlugin(pluginConfig)).toBe(false);
expect(logError).toHaveBeenCalledWith('There is a config with an invalid PLUGIN_OPERATION. Check to make sure it is configured correctly.');
});
});
});