Canvas AI: Restrict component catalog to canvas_test_sdc components when canvas_ai_agents_test is installed
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3591516. -->
Reported by: [akhil babu](https://www.drupal.org/user/3632866)
Related to !1152
>>>
<h3 id="overview">Overview</h3>
<p>The <code>canvas_ai_agents_test</code> module exists to provide automated test coverage for the Canvas AI agents. <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/canvas/-/work_items/3582390" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/canvas/-/work_items/3582390</a></span> adds automated tests for the orchestrator agent. The next step is to add tests for the page builder agent and the template builder agent.</p>
<p>Both agents call the <code>get_component_context</code> tool, which returns a catalog of every <code>component</code> entity available on the site. Because the catalog is site-specific, different test environments will see different components, making assertions non-deterministic.</p>
<p>To fix this, we need a way to lock the catalog down to a known set of components when running tests. Components provided by the existing <code>canvas_test_sdc</code> module are a good fit: they are part of the codebase, cover a variety of prop types, and are already used in other kernel tests.</p>
<h3 id="proposed-resolution">Proposed resolution</h3>
<h4>1. Add an entity access hook to <code>canvas_ai_agents_test</code></h4>
<p>Implement <code>hook_entity_access()</code> (or <code>hook_ENTITY_TYPE_access()</code> for <code>component</code>) in <code>canvas_ai_agents_test module</code>. The hook should return <code>AccessResult::forbidden()</code> for any <code>component</code> entity whose provider is not <code>canvas_test_sdc</code>. This restricts the visible component catalog to only the SDCs defined by <code>canvas_test_sdc</code> while the test module is installed, without touching any other module's data.</p>
<h4>2. Add a kernel test to <code>canvas_ai</code></h4>
<p>Add a kernel test class in the <code>canvas_ai</code> module (under <code>modules/canvas_ai/tests/src/Kernel/</code>) that covers the following sequence:</p>
<ol>
<li>Boot a kernel with <code>canvas_ai</code> installed but <strong>without</strong> <code>canvas_ai_agents_test</code>. Call <code>get_component_context</code> (via <code>CanvasAiPageBuilderHelper::getComponentContextForAi()</code>) and assert that the catalog contains components from sources other than <code>canvas_test_sdc</code> (for example, block components).</li>
<li>Install <code>canvas_ai_agents_test</code> into the same kernel. Call <code>get_component_context</code> again and assert that the catalog contains <em>only</em> components whose provider is <code>canvas_test_sdc</code>.</li>
<li>Uninstall <code>canvas_ai_agents_test</code>. Call <code>get_component_context</code> once more and assert that the broader component set is visible again.</li>
</ol>
<h4>Manual verification steps</h4>
<ol>
<li>Install the <code>canvas_ai_agents_test</code> module on a development site that has <code>canvas_ai</code> enabled.</li>
<li>Install the <code>ai_api_explorer</code> module.</li>
<li>Go to <code>/admin/config/ai/explorers/tools_explorer</code>, select the <code>get_component_context</code> tool, and click Run.</li>
<li>Verify that the output contains only SDCs from <code>canvas_test_sdc</code> and no blocks or other components.</li>
<li>Uninstall <code>canvas_ai_agents_test</code>, run the tool again, and verify the full component catalog is restored.</li>
</ol>
<h3 id="remaining-tasks">Remaining tasks</h3>
<ul>
<li>Add the <code>hook_entity_access</code> implementation in <code>canvas_ai_agents_test module</code>.</li>
<li>Add the kernel test class in <code>modules/canvas_ai/tests/src/Kernel/</code> covering the before/after/uninstall sequence described above.</li>
<li>Run PHPStan and PHPCS on all modified files and resolve any findings.</li>
<li>Manually verify using <code>ai_api_explorer</code> as described above.</li>
<li>Once this is in place, create a follow-up issue to add fixtures for the 'current layout' context used by the page builder and template builder agents (needed before writing the actual agent tests).</li>
</ul>
<p><em>Issue description generated with AI assistance.</em> </p>
> Related issue: [Issue #3591457](https://www.drupal.org/node/3591457)
> Related issue: [Issue #3579471](https://www.drupal.org/node/3579471)
issue