Skip to content

Commit

Permalink
Refactor Summernote widget and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Nov 27, 2023
1 parent bc1d01d commit 326e81f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Summernote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 Down Expand Up @@ -78,16 +79,16 @@ private function renderTextArea(): string
{
unset($this->options['id']);

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

return match ($this->hasModel()) {
true => $textArea
->content(Html::getAttributeValue($this->model, $this->attribute))

Check failure on line 86 in src/Summernote.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1-ubuntu-latest

Parameter #1 ...$values of method PHPForge\Html\Base\AbstractElement::content() expects PHPForge\Widget\ElementInterface|string, array|string given.

Check failure on line 86 in src/Summernote.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1-ubuntu-latest

Parameter #1 ...$values of method PHPForge\Html\Base\AbstractElement::content() expects PHPForge\Widget\ElementInterface|string, array|string given.
->id($this->id)
->name(Utils::generateInputName($this->model->formName(), $this->attribute))
->render(),
default => $textArea
->content($this->value ?? '')

Check failure on line 91 in src/Summernote.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1-ubuntu-latest

Property yii\widgets\InputWidget::$value (string) on left side of ?? is not nullable.

Check failure on line 91 in src/Summernote.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1-ubuntu-latest

Property yii\widgets\InputWidget::$value (string) on left side of ?? is not nullable.
->id($this->id)
->name($this->name)
->render(),
Expand Down
57 changes: 57 additions & 0 deletions 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 @@ -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,
);
}
}

0 comments on commit 326e81f

Please sign in to comment.