How to use Hook class

Best Cucumber Common Library code snippet using Hook

EntityCrudHookTest.php

Source: EntityCrudHookTest.php Github

copy

Full Screen

...27 * These hooks are each tested for several entity types.28 *29 * @group Entity30 */​31class EntityCrudHookTest extends EntityKernelTestBase {32 use CommentTestTrait;33 /​**34 * Modules to enable.35 *36 * @var array37 */​38 protected static $modules = [39 'block',40 'block_test',41 'entity_crud_hook_test',42 'file',43 'taxonomy',44 'node',45 'comment',46 ];47 protected $ids = [];48 protected function setUp(): void {49 parent::setUp();50 $this->installSchema('user', ['users_data']);51 $this->installSchema('file', ['file_usage']);52 $this->installSchema('node', ['node_access']);53 $this->installSchema('comment', ['comment_entity_statistics']);54 $this->installConfig(['node', 'comment']);55 }56 /​**57 * Checks the order of CRUD hook execution messages.58 *59 * Module entity_crud_hook_test implements all core entity CRUD hooks and60 * stores a message for each in $GLOBALS['entity_crud_hook_test'].61 *62 * @param array $messages63 * An array of plain-text messages in the order they should appear.64 *65 * @internal66 */​67 protected function assertHookMessageOrder(array $messages): void {68 $positions = [];69 foreach ($messages as $message) {70 /​/​ Verify that each message is found and record its position.71 $position = array_search($message, $GLOBALS['entity_crud_hook_test']);72 $this->assertNotFalse($position, $message);73 $positions[] = $position;74 }75 /​/​ Sort the positions and ensure they remain in the same order.76 $sorted = $positions;77 sort($sorted);78 $this->assertSame($positions, $sorted, 'The hook messages appear in the correct order.');79 }80 /​**81 * Tests hook invocations for CRUD operations on blocks.82 */​83 public function testBlockHooks() {84 $entity = Block::create([85 'id' => 'stark_test_html',86 'plugin' => 'test_html',87 'theme' => 'stark',88 ]);89 $this->assertHookMessageOrder([90 'entity_crud_hook_test_block_create called',91 'entity_crud_hook_test_entity_create called for type block',92 ]);93 $GLOBALS['entity_crud_hook_test'] = [];94 $entity->save();95 $this->assertHookMessageOrder([96 'entity_crud_hook_test_block_presave called',97 'entity_crud_hook_test_entity_presave called for type block',98 'entity_crud_hook_test_block_insert called',99 'entity_crud_hook_test_entity_insert called for type block',100 ]);101 $GLOBALS['entity_crud_hook_test'] = [];102 $entity = Block::load($entity->id());103 $this->assertHookMessageOrder([104 'entity_crud_hook_test_entity_load called for type block',105 'entity_crud_hook_test_block_load called',106 ]);107 $GLOBALS['entity_crud_hook_test'] = [];108 $entity->label = 'New label';109 $entity->save();110 $this->assertHookMessageOrder([111 'entity_crud_hook_test_block_presave called',112 'entity_crud_hook_test_entity_presave called for type block',113 'entity_crud_hook_test_block_update called',114 'entity_crud_hook_test_entity_update called for type block',115 ]);116 $GLOBALS['entity_crud_hook_test'] = [];117 $entity->delete();118 $this->assertHookMessageOrder([119 'entity_crud_hook_test_block_predelete called',120 'entity_crud_hook_test_entity_predelete called for type block',121 'entity_crud_hook_test_block_delete called',122 'entity_crud_hook_test_entity_delete called for type block',123 ]);124 }125 /​**126 * Tests hook invocations for CRUD operations on comments.127 */​128 public function testCommentHooks() {129 $account = $this->createUser();130 NodeType::create([131 'type' => 'article',132 'name' => 'Article',133 ])->save();134 $this->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN);135 $node = Node::create([136 'uid' => $account->id(),137 'type' => 'article',138 'title' => 'Test node',139 'status' => 1,140 'promote' => 0,141 'sticky' => 0,142 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,143 'created' => REQUEST_TIME,144 'changed' => REQUEST_TIME,145 ]);146 $node->save();147 $nid = $node->id();148 $GLOBALS['entity_crud_hook_test'] = [];149 $comment = Comment::create([150 'cid' => NULL,151 'pid' => 0,152 'entity_id' => $nid,153 'entity_type' => 'node',154 'field_name' => 'comment',155 'uid' => $account->id(),156 'subject' => 'Test comment',157 'created' => REQUEST_TIME,158 'changed' => REQUEST_TIME,159 'status' => 1,160 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,161 ]);162 $this->assertHookMessageOrder([163 'entity_crud_hook_test_comment_create called',164 'entity_crud_hook_test_entity_create called for type comment',165 ]);166 $GLOBALS['entity_crud_hook_test'] = [];167 $comment->save();168 $this->assertHookMessageOrder([169 'entity_crud_hook_test_comment_presave called',170 'entity_crud_hook_test_entity_presave called for type comment',171 'entity_crud_hook_test_comment_insert called',172 'entity_crud_hook_test_entity_insert called for type comment',173 ]);174 $GLOBALS['entity_crud_hook_test'] = [];175 $comment = Comment::load($comment->id());176 $this->assertHookMessageOrder([177 'entity_crud_hook_test_entity_load called for type comment',178 'entity_crud_hook_test_comment_load called',179 ]);180 $GLOBALS['entity_crud_hook_test'] = [];181 $comment->setSubject('New subject');182 $comment->save();183 $this->assertHookMessageOrder([184 'entity_crud_hook_test_comment_presave called',185 'entity_crud_hook_test_entity_presave called for type comment',186 'entity_crud_hook_test_comment_update called',187 'entity_crud_hook_test_entity_update called for type comment',188 ]);189 $GLOBALS['entity_crud_hook_test'] = [];190 $comment->delete();191 $this->assertHookMessageOrder([192 'entity_crud_hook_test_comment_predelete called',193 'entity_crud_hook_test_entity_predelete called for type comment',194 'entity_crud_hook_test_comment_delete called',195 'entity_crud_hook_test_entity_delete called for type comment',196 ]);197 }198 /​**199 * Tests hook invocations for CRUD operations on files.200 */​201 public function testFileHooks() {202 $this->installEntitySchema('file');203 $url = 'public:/​/​entity_crud_hook_test.file';204 file_put_contents($url, 'Test test test');205 $file = File::create([206 'fid' => NULL,207 'uid' => 1,208 'filename' => 'entity_crud_hook_test.file',209 'uri' => $url,210 'filemime' => 'text/​plain',211 'filesize' => filesize($url),212 'status' => 1,213 'created' => REQUEST_TIME,214 'changed' => REQUEST_TIME,215 ]);216 $this->assertHookMessageOrder([217 'entity_crud_hook_test_file_create called',218 'entity_crud_hook_test_entity_create called for type file',219 ]);220 $GLOBALS['entity_crud_hook_test'] = [];221 $file->save();222 $this->assertHookMessageOrder([223 'entity_crud_hook_test_file_presave called',224 'entity_crud_hook_test_entity_presave called for type file',225 'entity_crud_hook_test_file_insert called',226 'entity_crud_hook_test_entity_insert called for type file',227 ]);228 $GLOBALS['entity_crud_hook_test'] = [];229 $file = File::load($file->id());230 $this->assertHookMessageOrder([231 'entity_crud_hook_test_entity_load called for type file',232 'entity_crud_hook_test_file_load called',233 ]);234 $GLOBALS['entity_crud_hook_test'] = [];235 $file->setFilename('new.entity_crud_hook_test.file');236 $file->save();237 $this->assertHookMessageOrder([238 'entity_crud_hook_test_file_presave called',239 'entity_crud_hook_test_entity_presave called for type file',240 'entity_crud_hook_test_file_update called',241 'entity_crud_hook_test_entity_update called for type file',242 ]);243 $GLOBALS['entity_crud_hook_test'] = [];244 $file->delete();245 $this->assertHookMessageOrder([246 'entity_crud_hook_test_file_predelete called',247 'entity_crud_hook_test_entity_predelete called for type file',248 'entity_crud_hook_test_file_delete called',249 'entity_crud_hook_test_entity_delete called for type file',250 ]);251 }252 /​**253 * Tests hook invocations for CRUD operations on nodes.254 */​255 public function testNodeHooks() {256 $account = $this->createUser();257 $node = Node::create([258 'uid' => $account->id(),259 'type' => 'article',260 'title' => 'Test node',261 'status' => 1,262 'promote' => 0,263 'sticky' => 0,264 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,265 'created' => REQUEST_TIME,266 'changed' => REQUEST_TIME,267 ]);268 $this->assertHookMessageOrder([269 'entity_crud_hook_test_node_create called',270 'entity_crud_hook_test_entity_create called for type node',271 ]);272 $GLOBALS['entity_crud_hook_test'] = [];273 $node->save();274 $this->assertHookMessageOrder([275 'entity_crud_hook_test_node_presave called',276 'entity_crud_hook_test_entity_presave called for type node',277 'entity_crud_hook_test_node_insert called',278 'entity_crud_hook_test_entity_insert called for type node',279 ]);280 $GLOBALS['entity_crud_hook_test'] = [];281 $node = Node::load($node->id());282 $this->assertHookMessageOrder([283 'entity_crud_hook_test_entity_preload called for type node',284 'entity_crud_hook_test_entity_load called for type node',285 'entity_crud_hook_test_node_load called',286 ]);287 $GLOBALS['entity_crud_hook_test'] = [];288 $node->title = 'New title';289 $node->save();290 $this->assertHookMessageOrder([291 'entity_crud_hook_test_node_presave called',292 'entity_crud_hook_test_entity_presave called for type node',293 'entity_crud_hook_test_node_update called',294 'entity_crud_hook_test_entity_update called for type node',295 ]);296 $GLOBALS['entity_crud_hook_test'] = [];297 $node->delete();298 $this->assertHookMessageOrder([299 'entity_crud_hook_test_node_predelete called',300 'entity_crud_hook_test_entity_predelete called for type node',301 'entity_crud_hook_test_node_delete called',302 'entity_crud_hook_test_entity_delete called for type node',303 ]);304 }305 /​**306 * Tests hook invocations for CRUD operations on taxonomy terms.307 */​308 public function testTaxonomyTermHooks() {309 $this->installEntitySchema('taxonomy_term');310 $vocabulary = Vocabulary::create([311 'name' => 'Test vocabulary',312 'vid' => 'test',313 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,314 'description' => NULL,315 'module' => 'entity_crud_hook_test',316 ]);317 $vocabulary->save();318 $GLOBALS['entity_crud_hook_test'] = [];319 $term = Term::create([320 'vid' => $vocabulary->id(),321 'name' => 'Test term',322 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,323 'description' => NULL,324 'format' => 1,325 ]);326 $this->assertHookMessageOrder([327 'entity_crud_hook_test_taxonomy_term_create called',328 'entity_crud_hook_test_entity_create called for type taxonomy_term',329 ]);330 $GLOBALS['entity_crud_hook_test'] = [];331 $term->save();332 $this->assertHookMessageOrder([333 'entity_crud_hook_test_taxonomy_term_presave called',334 'entity_crud_hook_test_entity_presave called for type taxonomy_term',335 'entity_crud_hook_test_taxonomy_term_insert called',336 'entity_crud_hook_test_entity_insert called for type taxonomy_term',337 ]);338 $GLOBALS['entity_crud_hook_test'] = [];339 $term = Term::load($term->id());340 $this->assertHookMessageOrder([341 'entity_crud_hook_test_entity_load called for type taxonomy_term',342 'entity_crud_hook_test_taxonomy_term_load called',343 ]);344 $GLOBALS['entity_crud_hook_test'] = [];345 $term->setName('New name');346 $term->save();347 $this->assertHookMessageOrder([348 'entity_crud_hook_test_taxonomy_term_presave called',349 'entity_crud_hook_test_entity_presave called for type taxonomy_term',350 'entity_crud_hook_test_taxonomy_term_update called',351 'entity_crud_hook_test_entity_update called for type taxonomy_term',352 ]);353 $GLOBALS['entity_crud_hook_test'] = [];354 $term->delete();355 $this->assertHookMessageOrder([356 'entity_crud_hook_test_taxonomy_term_predelete called',357 'entity_crud_hook_test_entity_predelete called for type taxonomy_term',358 'entity_crud_hook_test_taxonomy_term_delete called',359 'entity_crud_hook_test_entity_delete called for type taxonomy_term',360 ]);361 }362 /​**363 * Tests hook invocations for CRUD operations on taxonomy vocabularies.364 */​365 public function testTaxonomyVocabularyHooks() {366 $this->installEntitySchema('taxonomy_term');367 $vocabulary = Vocabulary::create([368 'name' => 'Test vocabulary',369 'vid' => 'test',370 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,371 'description' => NULL,372 'module' => 'entity_crud_hook_test',373 ]);374 $this->assertHookMessageOrder([375 'entity_crud_hook_test_taxonomy_vocabulary_create called',376 'entity_crud_hook_test_entity_create called for type taxonomy_vocabulary',377 ]);378 $GLOBALS['entity_crud_hook_test'] = [];379 $vocabulary->save();380 $this->assertHookMessageOrder([381 'entity_crud_hook_test_taxonomy_vocabulary_presave called',382 'entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary',383 'entity_crud_hook_test_taxonomy_vocabulary_insert called',384 'entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary',385 ]);386 $GLOBALS['entity_crud_hook_test'] = [];387 $vocabulary = Vocabulary::load($vocabulary->id());388 $this->assertHookMessageOrder([389 'entity_crud_hook_test_entity_load called for type taxonomy_vocabulary',390 'entity_crud_hook_test_taxonomy_vocabulary_load called',391 ]);392 $GLOBALS['entity_crud_hook_test'] = [];393 $vocabulary->set('name', 'New name');394 $vocabulary->save();395 $this->assertHookMessageOrder([396 'entity_crud_hook_test_taxonomy_vocabulary_presave called',397 'entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary',398 'entity_crud_hook_test_taxonomy_vocabulary_update called',399 'entity_crud_hook_test_entity_update called for type taxonomy_vocabulary',400 ]);401 $GLOBALS['entity_crud_hook_test'] = [];402 $vocabulary->delete();403 $this->assertHookMessageOrder([404 'entity_crud_hook_test_taxonomy_vocabulary_predelete called',405 'entity_crud_hook_test_entity_predelete called for type taxonomy_vocabulary',406 'entity_crud_hook_test_taxonomy_vocabulary_delete called',407 'entity_crud_hook_test_entity_delete called for type taxonomy_vocabulary',408 ]);409 }410 /​**411 * Tests hook invocations for CRUD operations on users.412 */​413 public function testUserHooks() {414 $account = User::create([415 'name' => 'Test user',416 'mail' => 'test@example.com',417 'created' => REQUEST_TIME,418 'status' => 1,419 'language' => 'en',420 ]);421 $this->assertHookMessageOrder([422 'entity_crud_hook_test_user_create called',423 'entity_crud_hook_test_entity_create called for type user',424 ]);425 $GLOBALS['entity_crud_hook_test'] = [];426 $account->save();427 $this->assertHookMessageOrder([428 'entity_crud_hook_test_user_presave called',429 'entity_crud_hook_test_entity_presave called for type user',430 'entity_crud_hook_test_user_insert called',431 'entity_crud_hook_test_entity_insert called for type user',432 ]);433 $GLOBALS['entity_crud_hook_test'] = [];434 User::load($account->id());435 $this->assertHookMessageOrder([436 'entity_crud_hook_test_entity_load called for type user',437 'entity_crud_hook_test_user_load called',438 ]);439 $GLOBALS['entity_crud_hook_test'] = [];440 $account->name = 'New name';441 $account->save();442 $this->assertHookMessageOrder([443 'entity_crud_hook_test_user_presave called',444 'entity_crud_hook_test_entity_presave called for type user',445 'entity_crud_hook_test_user_update called',446 'entity_crud_hook_test_entity_update called for type user',447 ]);448 $GLOBALS['entity_crud_hook_test'] = [];449 $account->delete();450 $this->assertHookMessageOrder([451 'entity_crud_hook_test_user_predelete called',452 'entity_crud_hook_test_entity_predelete called for type user',453 'entity_crud_hook_test_user_delete called',454 'entity_crud_hook_test_entity_delete called for type user',455 ]);456 }457 /​**458 * Tests rollback from failed entity save.459 */​460 public function testEntityRollback() {461 /​/​ Create a block.462 try {463 EntityTest::create(['name' => 'fail_insert'])->save();464 $this->fail('Expected exception has not been thrown.');...

Full Screen

Full Screen

Hook

Using AI Code Generation

copy

Full Screen

1require_once 'CucumberCommon.php';2use Cucumber\Hook;3$hook = new Hook();4$hook->addBefore('BeforeTest', 'BeforeTest');5$hook->addAfter('AfterTest', 'AfterTest');6function BeforeTest()7{8 echo "Before Test";9}10function AfterTest()11{12 echo "After Test";13}14require_once 'CucumberCommon.php';15use Cucumber\Hook;16$hook = new Hook();17$hook->addBefore('BeforeTest', 'BeforeTest');18$hook->addAfter('AfterTest', 'AfterTest');19function BeforeTest()20{21 echo "Before Test";22}23function AfterTest()24{25 echo "After Test";26}27require_once 'CucumberCommon.php';28use Cucumber\Hook;29$hook = new Hook();30$hook->addBefore('BeforeTest', 'BeforeTest');31$hook->addAfter('AfterTest', 'AfterTest');32function BeforeTest()33{34 echo "Before Test";35}36function AfterTest()37{38 echo "After Test";39}40require_once 'CucumberCommon.php';41use Cucumber\Hook;42$hook = new Hook();43$hook->addBefore('BeforeTest', 'BeforeTest');44$hook->addAfter('AfterTest', 'AfterTest');45function BeforeTest()46{47 echo "Before Test";48}49function AfterTest()50{51 echo "After Test";52}53require_once 'CucumberCommon.php';54use Cucumber\Hook;55$hook = new Hook();56$hook->addBefore('BeforeTest', 'BeforeTest');57$hook->addAfter('AfterTest', 'AfterTest');58function BeforeTest()59{60 echo "Before Test";61}62function AfterTest()63{64 echo "After Test";65}66require_once 'CucumberCommon.php';67use Cucumber\Hook;68$hook = new Hook();69$hook->addBefore('BeforeTest', 'BeforeTest');70$hook->addAfter('AfterTest', 'AfterTest');71function BeforeTest()72{

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

QA Innovation – Using the senseshaping concept to discover customer needs

QA Innovation - Using the senseshaping concept to discover customer needsQA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.

How To Write End-To-End Tests Using Cypress App Actions

When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.

A Complete Guide To CSS Grid

Ever since the Internet was invented, web developers have searched for the most efficient ways to display content on web browsers.

A Comprehensive Guide On JUnit 5 Extensions

JUnit is one of the most popular unit testing frameworks in the Java ecosystem. The JUnit 5 version (also known as Jupiter) contains many exciting innovations, including support for new features in Java 8 and above. However, many developers still prefer to use the JUnit 4 framework since certain features like parallel execution with JUnit 5 are still in the experimental phase.

Complete Guide To Styling Forms With CSS Accent Color

The web paradigm has changed considerably over the last few years. Web 2.0, a term coined way back in 1999, was one of the pivotal moments in the history of the Internet. UGC (User Generated Content), ease of use, and interoperability for the end-users were the key pillars of Web 2.0. Consumers who were only consuming content up till now started creating different forms of content (e.g., text, audio, video, etc.).

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cucumber Common Library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful