Skip to content

Refactor Summernote widget and add tests. #9

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"require": {
"php": ">=8.1",
"oomphinc/composer-installers-extender": "^2.0",
"php-forge/html": "dev-main",
"yii2-extensions/asset-bootstrap5": "dev-main",
"yiisoft/yii2": "*"
},
Expand Down
21 changes: 5 additions & 16 deletions src/Summernote.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace Yii2\Extensions\Summernote;

use JsonException;
use PHPForge\Html\Helper\Utils;
use PHPForge\Html\TextArea;
use Yii2\Extensions\Summernote\Asset\SummernoteAsset;
use Yii;
use yii\helpers\Html;
use yii\widgets\InputWidget;

final class Summernote extends InputWidget
Expand All @@ -29,7 +28,7 @@ public function init(): void

$this->config = array_merge(['lang' => Yii::$app->language], $this->config);
$this->id = $this->hasModel()
? Utils::generateInputId($this->model->formName(), $this->attribute)
? Html::getInputId($this->model, $this->attribute)
: $this->getId() . '-summernote';
}

Expand Down Expand Up @@ -76,21 +75,11 @@ private function registerClientScript(): void
*/
private function renderTextArea(): string
{
unset($this->options['id']);

$textArea = TextArea::widget()
->attributes($this->options)
->content((string) $this->value);
$this->options['id'] = $this->id;

return match ($this->hasModel()) {
true => $textArea
->id($this->id)
->name(Utils::generateInputName($this->model->formName(), $this->attribute))
->render(),
default => $textArea
->id($this->id)
->name($this->name)
->render(),
true => Html::activeTextArea($this->model, $this->attribute, $this->options),
default => Html::textArea($this->name, $this->value, $this->options),
};
}
}
59 changes: 58 additions & 1 deletion tests/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function setup(): void
parent::setUp();
$this->mockApplication();

Summernote::$counter = 0;

$this->view = Yii::$app->getView();
}

Expand Down Expand Up @@ -89,7 +91,7 @@ public function testOptions(): void

$this->assertSame(
<<<HTML
<textarea class="test-class" id="testform-content" name="TestForm[content]"></textarea>
<textarea id="testform-content" class="test-class" name="TestForm[content]"></textarea>
HTML,
$filePond,
);
Expand Down Expand Up @@ -120,4 +122,59 @@ public function testRender(): void
$result,
);
}

public function testValue(): void
{
$filePond = Summernote::widget(
[
'name' => 'content',
'value' => 'test-content',
],
);

$this->assertSame(
<<<HTML
<textarea id="w0-summernote" name="content">test-content</textarea>
HTML,
$filePond,
);

$result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]);

$this->assertStringContainsString(
<<<JS
$('#w0-summernote').summernote({"lang":"en-US"});
JS,
$result,
);
}

public function testValueWithModel(): void
{
$model = new TestForm();
$model->content = 'test-content';

$filePond = Summernote::widget(
[
'attribute' => 'content',
'model' => $model,
],
);

$this->assertSame(
<<<HTML
<textarea id="testform-content" name="TestForm[content]">test-content</textarea>
HTML,
$filePond,
);

$result = $this->view->renderFile(__DIR__ . '/Support/main.php', ['widget' => $filePond]);

$this->assertStringContainsString(
<<<JS
$('#testform-content').summernote({"lang":"en-US"});
JS,
$result,
);
}
}