Skip to content

Commit f07dd52

Browse files
authored
Convert the page break plugin to service providers (#40743)
* Convert the page break plugin to service providers * DEPLOY * test ---------
1 parent 65da463 commit f07dd52

File tree

4 files changed

+168
-10
lines changed

4 files changed

+168
-10
lines changed

plugins/editors-xtd/pagebreak/pagebreak.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
<authorUrl>www.joomla.org</authorUrl>
1010
<version>3.0.0</version>
1111
<description>PLG_EDITORSXTD_PAGEBREAK_XML_DESCRIPTION</description>
12+
<namespace path="src">Joomla\Plugin\EditorsXtd\PageBreak</namespace>
1213
<files>
13-
<filename plugin="pagebreak">pagebreak.php</filename>
14+
<folder plugin="pagebreak">services</folder>
15+
<folder>src</folder>
1416
</files>
1517
<languages>
1618
<language tag="en-GB">language/en-GB/plg_editors-xtd_pagebreak.ini</language>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage Editors-xtd.pagebreak
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
use Joomla\Event\DispatcherInterface;
19+
use Joomla\Plugin\EditorsXtd\PageBreak\Extension\PageBreak;
20+
21+
return new class () implements ServiceProviderInterface {
22+
/**
23+
* Registers the service provider with a DI container.
24+
*
25+
* @param Container $container The DI container.
26+
*
27+
* @return void
28+
*
29+
* @since __DEPLOY_VERSION__
30+
*/
31+
public function register(Container $container)
32+
{
33+
$container->set(
34+
PluginInterface::class,
35+
function (Container $container) {
36+
$dispatcher = $container->get(DispatcherInterface::class);
37+
38+
$plugin = new PageBreak(
39+
$dispatcher,
40+
(array) PluginHelper::getPlugin('editors-xtd', 'pagebreak')
41+
);
42+
$plugin->setApplication(Factory::getApplication());
43+
44+
return $plugin;
45+
}
46+
);
47+
}
48+
};

plugins/editors-xtd/pagebreak/pagebreak.php renamed to plugins/editors-xtd/pagebreak/src/Extension/PageBreak.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
*
77
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
88
* @license GNU General Public License version 2 or later; see LICENSE.txt
9-
10-
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
119
*/
1210

13-
use Joomla\CMS\Factory;
14-
use Joomla\CMS\Language\Text;
11+
namespace Joomla\Plugin\EditorsXtd\PageBreak\Extension;
12+
13+
use Joomla\CMS\Application\CMSWebApplicationInterface;
1514
use Joomla\CMS\Object\CMSObject;
1615
use Joomla\CMS\Plugin\CMSPlugin;
1716

@@ -24,7 +23,7 @@
2423
*
2524
* @since 1.5
2625
*/
27-
class PlgButtonPagebreak extends CMSPlugin
26+
final class PageBreak extends CMSPlugin
2827
{
2928
/**
3029
* Load the language file on instantiation.
@@ -45,14 +44,20 @@ class PlgButtonPagebreak extends CMSPlugin
4544
*/
4645
public function onDisplay($name)
4746
{
48-
$user = Factory::getUser();
47+
$app = $this->getApplication();
48+
49+
if (!$app instanceof CMSWebApplicationInterface) {
50+
return;
51+
}
52+
53+
$user = $app->getIdentity();
4954

5055
// Can create in any category (component permission) or at least in one category
5156
$canCreateRecords = $user->authorise('core.create', 'com_content')
5257
|| count($user->getAuthorisedCategories('com_content', 'core.create')) > 0;
5358

5459
// Instead of checking edit on all records, we can use **same** check as the form editing view
55-
$values = (array) Factory::getApplication()->getUserState('com_content.edit.article.id');
60+
$values = (array) $app->getUserState('com_content.edit.article.id');
5661
$isEditingRecords = count($values);
5762

5863
// This ACL check is probably a double-check (form view already performed checks)
@@ -61,13 +66,13 @@ public function onDisplay($name)
6166
return;
6267
}
6368

64-
Factory::getDocument()->addScriptOptions('xtd-pagebreak', ['editor' => $name]);
69+
$app->getDocument()->addScriptOptions('xtd-pagebreak', ['editor' => $name]);
6570
$link = 'index.php?option=com_content&amp;view=article&amp;layout=pagebreak&amp;tmpl=component&amp;e_name=' . $name;
6671

6772
$button = new CMSObject();
6873
$button->modal = true;
6974
$button->link = $link;
70-
$button->text = Text::_('PLG_EDITORSXTD_PAGEBREAK_BUTTON_PAGEBREAK');
75+
$button->text = $app->getLanguage()->_('PLG_EDITORSXTD_PAGEBREAK_BUTTON_PAGEBREAK');
7176
$button->name = $this->_type . '_' . $this->_name;
7277
$button->icon = 'copy';
7378
$button->iconSVG = '<svg viewBox="0 0 32 32" width="24" height="24"><path d="M26 8h-6v-2l-6-6h-14v24h12v8h20v-18l-6-6zM26 10.828l3.172 3'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.UnitTest
5+
* @subpackage Extension
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Tests\Unit\Plugin\EditorsXtd\PageBreak\Extension;
12+
13+
use Joomla\CMS\Application\CMSApplicationInterface;
14+
use Joomla\CMS\Application\CMSWebApplicationInterface;
15+
use Joomla\CMS\Document\Document;
16+
use Joomla\CMS\Language\Language;
17+
use Joomla\CMS\User\User;
18+
use Joomla\Event\Dispatcher;
19+
use Joomla\Plugin\EditorsXtd\PageBreak\Extension\PageBreak;
20+
use Joomla\Tests\Unit\UnitTestCase;
21+
22+
/**
23+
* Test class for PageBreak editor button plugin
24+
*
25+
* @package Joomla.UnitTest
26+
* @subpackage PageBreak
27+
*
28+
* @testdox The PageBreak plugin
29+
*
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
class PageBreakTest extends UnitTestCase
33+
{
34+
/**
35+
* @testdox that the button is properly loaded
36+
*
37+
* @return void
38+
*
39+
* @since __DEPLOY_VERSION__
40+
*/
41+
public function testButton()
42+
{
43+
$user = $this->createStub(User::class);
44+
$user->method('authorise')->willReturn(true);
45+
$user->method('getAuthorisedCategories')->willReturn([]);
46+
47+
$app = $this->createStub(CMSWebApplicationInterface::class);
48+
$app->method('getLanguage')->willReturn($this->createStub(Language::class));
49+
$app->method('getIdentity')->willReturn($user);
50+
$app->method('getDocument')->willReturn(new Document());
51+
52+
$dispatcher = new Dispatcher();
53+
$plugin = new PageBreak($dispatcher, ['params' => []]);
54+
$plugin->setApplication($app);
55+
$button = $plugin->onDisplay('test');
56+
57+
$this->assertNotNull($button);
58+
$this->assertNotEmpty($button->name);
59+
$this->assertNotEmpty($button->link);
60+
$this->assertNotEmpty($button->options);
61+
}
62+
63+
/**
64+
* @testdox that the user has not enough permission
65+
*
66+
* @return void
67+
*
68+
* @since __DEPLOY_VERSION__
69+
*/
70+
public function testInvalidPermissions()
71+
{
72+
$user = $this->createStub(User::class);
73+
$user->method('authorise')->willReturn(false);
74+
$user->method('getAuthorisedCategories')->willReturn([]);
75+
76+
$app = $this->createStub(CMSWebApplicationInterface::class);
77+
$app->method('getIdentity')->willReturn($user);
78+
79+
$dispatcher = new Dispatcher();
80+
$plugin = new PageBreak($dispatcher, ['params' => []]);
81+
$plugin->setApplication($app);
82+
$button = $plugin->onDisplay('test');
83+
84+
$this->assertNull($button);
85+
}
86+
87+
/**
88+
* @testdox that the button is not returned when a none web CMS application is used
89+
*
90+
* @return void
91+
*
92+
* @since __DEPLOY_VERSION__
93+
*/
94+
public function testInvalidApplication()
95+
{
96+
$dispatcher = new Dispatcher();
97+
$plugin = new PageBreak($dispatcher, ['params' => []]);
98+
$plugin->setApplication($this->createStub(CMSApplicationInterface::class));
99+
$button = $plugin->onDisplay('test');
100+
101+
$this->assertNull($button);
102+
}
103+
}

0 commit comments

Comments
 (0)