Re-add syntax highlighting to daily emails and
...ATDC lessons
This commit is contained in:
parent
0d9bb37503
commit
5fbf48d9ac
48 changed files with 186 additions and 165 deletions
|
|
@ -12,7 +12,7 @@ In this lesson, let's add tags to our posts using the `PostBuilder`.
|
|||
|
||||
As we're doing test-driven development, start by creating a new `PostBuilderTest`:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
<?php
|
||||
|
||||
// web/modules/custom/atdc/tests/src/Kernel/Builder/PostBuilderTest.php
|
||||
|
|
@ -37,7 +37,7 @@ Let's start by testing the existing functionality within `PostBuilder` by verify
|
|||
|
||||
Create these tests, which should pass by default as the code is already written:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
/** @test */
|
||||
public function it_returns_a_published_post(): void {
|
||||
$node = PostBuilder::create()
|
||||
|
|
@ -73,7 +73,7 @@ Next, create a test for adding tags to a post.
|
|||
|
||||
It should be mostly the same as the others, but instead of an assertion for the published status, try to use `var_dump()` to see the value of `field_tags`:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
/** @test */
|
||||
public function it_returns_a_post_with_tags(): void {
|
||||
$node = PostBuilder::create()
|
||||
|
|
@ -104,7 +104,7 @@ The convention is to create a test module that will only be required within the
|
|||
|
||||
To do this, create a `web/modules/custom/atdc/modules/atdc_test` directory and an `atdc_test.info.yml` file with this content:
|
||||
|
||||
```yaml
|
||||
```language-yaml
|
||||
name: ATDC Test
|
||||
type: module
|
||||
core_version_requirement: ^10
|
||||
|
|
@ -123,7 +123,7 @@ Rather than trying to write them by hand, I create the configuration I need, suc
|
|||
|
||||
To do that for this project, as I'm using the PHP built-in web server, I can use Drush to install Drupal using an SQLite database:
|
||||
|
||||
```plain
|
||||
```language-plain
|
||||
./vendor/bin/drush site:install --db-url sqlite://localhost/atdc.sqlite
|
||||
```
|
||||
|
||||
|
|
@ -143,7 +143,7 @@ These are the files that I created in my module based on the field I created.
|
|||
|
||||
`field.field.node.post.field_tags.yml`:
|
||||
|
||||
```yaml
|
||||
```language-yaml
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
|
|
@ -174,7 +174,7 @@ field_type: entity_reference
|
|||
|
||||
`field.storage.node.field_tags.yml`:
|
||||
|
||||
```yaml
|
||||
```language-yaml
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies:
|
||||
|
|
@ -198,7 +198,7 @@ custom_storage: false
|
|||
|
||||
Then, enable the module within `PostBuilderTest`:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
protected static $modules = [
|
||||
// Core.
|
||||
'node',
|
||||
|
|
@ -210,7 +210,7 @@ protected static $modules = [
|
|||
|
||||
Finally, install the configuration to create the field. Add this within the test:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
$this->installConfig(modules: [
|
||||
'atdc_test',
|
||||
]);
|
||||
|
|
@ -218,7 +218,7 @@ $this->installConfig(modules: [
|
|||
|
||||
After adding this and attempting to install the configuration to add the field, you'll get an error:
|
||||
|
||||
```plain
|
||||
```language-plain
|
||||
Exception when installing config for module atdc_test, the message was: Field 'field_tags' on entity type 'node' references a target entity type 'taxonomy_term', which does not exist.
|
||||
```
|
||||
|
||||
|
|
@ -234,7 +234,7 @@ As well as the field configuration, we also need to create the Post content type
|
|||
|
||||
This can be done by creating a `node.type.post.yml` file:
|
||||
|
||||
```yaml
|
||||
```language-yaml
|
||||
langcode: en
|
||||
status: true
|
||||
dependencies: { }
|
||||
|
|
@ -255,7 +255,7 @@ Let's update the test and add assertions about the tags being saved and returned
|
|||
|
||||
Get the tags from the post and assert that three tags are returned:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
$tags = $node->get('field_tags')->referencedEntities();
|
||||
self::assertCount(3, $tags);
|
||||
```
|
||||
|
|
@ -264,7 +264,7 @@ As none have been added, this would fail the test.
|
|||
|
||||
Update the test to use a `setTags()` method that you haven't created yet:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
$node = PostBuilder::create()
|
||||
->setTitle('test')
|
||||
->setTags(['Drupal', 'PHP', 'Testing'])
|
||||
|
|
@ -277,7 +277,7 @@ You should get an error confirming the method is undefined:
|
|||
|
||||
To fix this, add the `tags` property and `setTags()` method to `PostBuilder`:
|
||||
|
||||
```php
|
||||
```language-php
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
|
|
@ -297,7 +297,7 @@ Tags will be an array of strings, and `setTags()` should set the tags to the `ta
|
|||
|
||||
Next, add the logic to `getPost()` to create a taxonomy term for each tag name.
|
||||
|
||||
```php
|
||||
```language-php
|
||||
$tagTerms = [];
|
||||
|
||||
if ($this->tags !== []) {
|
||||
|
|
@ -322,7 +322,7 @@ If `$this->tags` is not empty, create a new taxonomy term for each one and save
|
|||
|
||||
As well as asserting we have the correct number of tags, let's also assert that the correct tag names are returned and that they are the correct type of term.
|
||||
|
||||
```php
|
||||
```language-php
|
||||
self::assertContainsOnlyInstancesOf(TermInterface::class, $tags);
|
||||
foreach ($tags as $tag) {
|
||||
self::assertSame('tags', $tag->bundle());
|
||||
|
|
@ -333,7 +333,7 @@ To assert the tags array only includes taxonomy terms, use `self::assertContains
|
|||
|
||||
Next, add some new assertions to the test to check the tag names match the specified tags.
|
||||
|
||||
```php
|
||||
```language-php
|
||||
self::assertSame('Drupal', $tags[0]->label());
|
||||
self::assertSame('PHP', $tags[1]->label());
|
||||
self::assertSame('Testing', $tags[2]->label());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue