From d6825866f341a3f5f9965a1b1f9dc0e7ffccdca4 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 15:28:33 +0200 Subject: [PATCH 01/79] feat: Add report to user posting rights Refs: #RW-1058, #RW-1059 --- .../reliefweb_fields/reliefweb_fields.install | 65 +++++++++++++++++++ .../reliefweb_fields/reliefweb_fields.module | 1 + .../FieldType/ReliefWebUserPostingRights.php | 24 +++++++ .../ReliefWebUserPostingRights.php | 12 +++- .../src/Helpers/UserPostingRightsHelper.php | 11 ++-- 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 html/modules/custom/reliefweb_fields/reliefweb_fields.install diff --git a/html/modules/custom/reliefweb_fields/reliefweb_fields.install b/html/modules/custom/reliefweb_fields/reliefweb_fields.install new file mode 100644 index 000000000..3d2d88028 --- /dev/null +++ b/html/modules/custom/reliefweb_fields/reliefweb_fields.install @@ -0,0 +1,65 @@ +get('taxonomy_term.field_schema_data.field_user_posting_rights'); + foreach ($kv_schema as $table => &$info) { + $schema = Database::getConnection()->schema(); + if (!$schema->fieldExists($table, 'field_user_posting_rights_report')) { + $spec = [ + 'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => '', + 'default' => 0, + ]; + $schema->addField($table, 'field_user_posting_rights_report', $spec); + + $spec = [ + 'fields' => [ + 'field_user_posting_rights_report' => [ + 'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => '', + 'default' => 0, + ], + ], + 'indexes' => [ + 'field_user_posting_rights_report' => ['field_user_posting_rights_report'], + ], + ]; + + $schema->addIndex($table, 'field_user_posting_rights_report', [ + 'field_user_posting_rights_report', + ], $spec); + } + + $info['fields']['field_user_posting_rights_report'] = [ + 'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => '', + 'default' => 0, + ]; + $info['indexes']['field_user_posting_rights_report'] = [ + 'field_user_posting_rights_report', + ]; + } + + \Drupal::keyValue('entity.storage_schema.sql')->set('taxonomy_term.field_schema_data.field_user_posting_rights', $kv_schema); + + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition('field_user_posting_rights', 'taxonomy_term'); + $field_storage_definition->setSetting('column_changes_handled', TRUE); + $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition); +} diff --git a/html/modules/custom/reliefweb_fields/reliefweb_fields.module b/html/modules/custom/reliefweb_fields/reliefweb_fields.module index 5ffa526ad..2eb0de199 100644 --- a/html/modules/custom/reliefweb_fields/reliefweb_fields.module +++ b/html/modules/custom/reliefweb_fields/reliefweb_fields.module @@ -5,6 +5,7 @@ * Module file for the ReliefWeb Fields module. */ +use Drupal\Core\Database\Database; use Drupal\Core\Entity\EntityInterface; use Drupal\filter\FilterFormatInterface; use Drupal\reliefweb_fields\Plugin\Field\FieldWidget\ReliefWebLinks; diff --git a/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldType/ReliefWebUserPostingRights.php b/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldType/ReliefWebUserPostingRights.php index 39ecbedea..c356821b2 100644 --- a/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldType/ReliefWebUserPostingRights.php +++ b/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldType/ReliefWebUserPostingRights.php @@ -50,6 +50,13 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'not null' => TRUE, 'default' => 0, ], + 'report' => [ + 'description' => 'Report posting rights: 0 = unverified; 1 = blocked; 2 = allowed; 3 = trusted.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ], 'notes' => [ 'description' => 'Notes', 'type' => 'text', @@ -60,6 +67,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'id' => ['id'], 'job' => ['job'], 'training' => ['training'], + 'report' => ['report'], ], ]; } @@ -80,6 +88,10 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel(new TranslatableMarkup('Training')) ->setRequired(FALSE); + $properties['report'] = DataDefinition::create('integer') + ->setLabel(new TranslatableMarkup('Report')) + ->setRequired(FALSE); + $properties['notes'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('Notes')) ->setRequired(FALSE); @@ -136,6 +148,17 @@ public function getConstraints() { ], ], ]); + $constraints[] = $constraint_manager->create('ComplexData', [ + 'report' => [ + 'AllowedValues' => [ + 'choices' => [0, 1, 2, 3], + 'strict' => TRUE, + 'message' => $this->t('%name: the Report rights must be one of 0, 1, 2 or 3.', [ + '%name' => $this->getFieldDefinition()->getLabel(), + ]), + ], + ], + ]); return $constraints; } @@ -147,6 +170,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin $values['url'] = mt_rand(3, 1000000); $values['job'] = mt_rand(0, 3); $values['training'] = mt_rand(0, 3); + $values['report'] = mt_rand(0, 3); return $values; } diff --git a/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldWidget/ReliefWebUserPostingRights.php b/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldWidget/ReliefWebUserPostingRights.php index 227ece21c..55c795ec8 100644 --- a/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldWidget/ReliefWebUserPostingRights.php +++ b/html/modules/custom/reliefweb_fields/src/Plugin/Field/FieldWidget/ReliefWebUserPostingRights.php @@ -174,6 +174,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface 'id' => intval($item['id'], 10), 'job' => intval($item['job'], 10), 'training' => intval($item['training'], 10), + 'report' => intval($item['report'], 10), 'notes' => $item['notes'], ]; } @@ -200,6 +201,7 @@ public static function normalizeData(array $data) { $data['id'] = intval($data['id'] ?? $data['uid'], 10); $data['job'] = isset($data['job']) ? intval($data['job'], 10) : 0; $data['training'] = isset($data['training']) ? intval($data['training'], 10) : 0; + $data['report'] = isset($data['report']) ? intval($data['report'], 10) : 0; $data['notes'] = isset($data['notes']) ? trim($data['notes']) : ''; $data['name'] = trim($data['name']); @@ -210,6 +212,7 @@ public static function normalizeData(array $data) { if ($data['status'] === 0) { $data['job'] = 1; $data['training'] = 1; + $data['report'] = 1; } return $data; @@ -410,7 +413,8 @@ public static function updateFields($op, UserInterface $user) { $query->condition($query ->orConditionGroup() ->condition($field_name . '.job', 1, '<>') - ->condition($field_name . '.training', 1, '<>')); + ->condition($field_name . '.training', 1, '<>') + ->condition($field_name . '.report', 1, '<>')); } $ids = $query?->execute(); @@ -435,9 +439,10 @@ public static function updateFields($op, UserInterface $user) { } // Set the rights to 'blocked' if the account is blocked. elseif ($blocked) { - if ($item['job'] != 1 || $item['training'] != 1) { + if ($item['job'] != 1 || $item['training'] != 1 || $item['report'] != 1) { $items[$delta]['job'] = 1; $items[$delta]['training'] = 1; + $items[$delta]['report'] = 1; if (!empty($item['notes'])) { $items[$delta]['notes'] .= ' ' . $message; } @@ -450,9 +455,10 @@ public static function updateFields($op, UserInterface $user) { // Reset the rights to 'unverified' if the email changed but // preserve the 'blocked' rights. elseif ($email_changed) { - if ($item['job'] > 1 || $item['training'] > 1) { + if ($item['job'] > 1 || $item['training'] > 1 || $item['report'] > 1) { $items[$delta]['job'] = $item['job'] == 1 ? 1 : 0; $items[$delta]['training'] = $item['training'] == 1 ? 1 : 0; + $items[$delta]['report'] = $item['report'] == 1 ? 1 : 0; if (!empty($item['notes'])) { $items[$delta]['notes'] .= ' ' . $message; } diff --git a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php index f059d1862..65fb7425b 100644 --- a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php +++ b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php @@ -117,12 +117,14 @@ public static function getUserPostingRights(?AccountInterface $account = NULL, a $id_field = $helper->getFieldColumnName('taxonomy_term', 'field_user_posting_rights', 'id'); $job_field = $helper->getFieldColumnName('taxonomy_term', 'field_user_posting_rights', 'job'); $training_field = $helper->getFieldColumnName('taxonomy_term', 'field_user_posting_rights', 'training'); + $report_field = $helper->getFieldColumnName('taxonomy_term', 'field_user_posting_rights', 'report'); // Get the rights associated with the user id. $query = $helper->getDatabase()->select($table, $table); $query->addField($table, 'entity_id', 'tid'); $query->addField($table, $job_field, 'job'); $query->addField($table, $training_field, 'training'); + $query->addField($table, $report_field, 'report'); $query->condition($table . '.bundle', 'source', '='); $query->condition($table . '.' . $id_field, $account->id(), '='); if (!empty($sources)) { @@ -142,6 +144,7 @@ public static function getUserPostingRights(?AccountInterface $account = NULL, a 'tid' => $tid, 'job' => 0, 'training' => 0, + 'report' => 0, ]; } } @@ -158,7 +161,7 @@ public static function getUserPostingRights(?AccountInterface $account = NULL, a * Compute the "final" posting right for a document based on an account's * rights for the given sources. * - * Currently only for jobs and training. + * Currently only for jobs, trainings and reports. * * @param \Drupal\Core\Session\AccountInterface $account * A user's account object or the current user if NULL. @@ -173,7 +176,7 @@ public static function getUserPostingRights(?AccountInterface $account = NULL, a */ public static function getUserConsolidatedPostingRight(AccountInterface $account, $bundle, array $sources) { // Not a job nor training or no sources, consider the user 'unverified'. - if (empty($account->uid) || ($bundle !== 'job' && $bundle !== 'training') || empty($sources)) { + if (empty($account->uid) || ($bundle !== 'job' && $bundle !== 'training' && $bundle !== 'report') || empty($sources)) { return [ 'code' => 0, 'name' => 'unverified', @@ -251,8 +254,8 @@ public static function userHasPostingRights(AccountInterface $account, EntityInt $owner = $entity->getOwnerId() === $account->id() && $account->id() > 0; } - // Only applies to job and training. - if ($bundle === 'job' || $bundle === 'training') { + // Only applies to job, training and report. + if ($bundle === 'job' || $bundle === 'training' || $bundle === 'report') { // Check for sources for which the user is blocked, allowed or trusted. // // Note: if there is no source or the user in unverified for the sources From bb17ae485ac314f3118b859a3e209e114053d283 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 15:39:33 +0200 Subject: [PATCH 02/79] feat: Add report to user posting rights Refs: #RW-1058, #RW-1059 --- .../reliefweb-user-posting-rights.css | 31 +++++++++++++++---- .../reliefweb-user-posting-rights.js | 7 ++++- .../reliefweb_fields/reliefweb_fields.module | 1 - 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.css b/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.css index 2f1bd063e..254b8e659 100644 --- a/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.css +++ b/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.css @@ -41,7 +41,7 @@ flex-wrap: wrap; align-items: center; } -.field--type-reliefweb-user-posting-rights div[data-filters][data-job="all"][data-training="all"] ~ ul li { +.field--type-reliefweb-user-posting-rights div[data-filters][data-job="all"][data-training="all"][data-report="all"] ~ ul li { display: flex; } .field--type-reliefweb-user-posting-rights div[data-filters][data-job="0"] ~ ul li[data-job="0"] { @@ -68,8 +68,22 @@ .field--type-reliefweb-user-posting-rights div[data-filters][data-training="3"] ~ ul li[data-training="3"] { display: flex; } +.field--type-reliefweb-user-posting-rights div[data-filters][data-report="0"] ~ ul li[data-report="0"] { + display: flex; +} +.field--type-reliefweb-user-posting-rights div[data-filters][data-report="1"] ~ ul li[data-report="1"] { + display: flex; +} +.field--type-reliefweb-user-posting-rights div[data-filters][data-report="2"] ~ ul li[data-report="2"] { + display: flex; +} +.field--type-reliefweb-user-posting-rights div[data-filters][data-report="3"] ~ ul li[data-report="3"] { + display: flex; +} + .field--type-reliefweb-user-posting-rights [data-job] label.job span:after, -.field--type-reliefweb-user-posting-rights [data-training] label.training span:after { +.field--type-reliefweb-user-posting-rights [data-training] label.training span:after, +.field--type-reliefweb-user-posting-rights [data-report] label.report span:after { display: inline-block; width: 12px; height: 12px; @@ -79,21 +93,26 @@ border-radius: 4px; } .field--type-reliefweb-user-posting-rights [data-job="0"] label.job span:after, -.field--type-reliefweb-user-posting-rights [data-training="0"] label.training span:after { +.field--type-reliefweb-user-posting-rights [data-training="0"] label.training span:after, +.field--type-reliefweb-user-posting-rights [data-report="0"] label.report span:after { background: #f49e2c; } .field--type-reliefweb-user-posting-rights [data-job="1"] label.job span:after, -.field--type-reliefweb-user-posting-rights [data-training="1"] label.training span:after { +.field--type-reliefweb-user-posting-rights [data-training="1"] label.training span:after, +.field--type-reliefweb-user-posting-rights [data-report="1"] label.report span:after { background: #da190b; } .field--type-reliefweb-user-posting-rights [data-job="2"] label.job span:after, -.field--type-reliefweb-user-posting-rights [data-training="2"] label.training span:after { +.field--type-reliefweb-user-posting-rights [data-training="2"] label.training span:after, +.field--type-reliefweb-user-posting-rights [data-report="2"] label.report span:after { background: #076d96; } .field--type-reliefweb-user-posting-rights [data-job="3"] label.job span:after, -.field--type-reliefweb-user-posting-rights [data-training="3"] label.training span:after { +.field--type-reliefweb-user-posting-rights [data-training="3"] label.training span:after, +.field--type-reliefweb-user-posting-rights [data-report="3"] label.report span:after { background: #88bb09; } + .field--type-reliefweb-user-posting-rights ul { margin: 0; padding: 0; diff --git a/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.js b/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.js index 13bf15e58..2ee8e1cf7 100644 --- a/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.js +++ b/html/modules/custom/reliefweb_fields/components/reliefweb-user-posting-rights/reliefweb-user-posting-rights.js @@ -117,6 +117,7 @@ container.setAttribute('data-status', data.status ? 'active' : 'blocked'); container.setAttribute('data-job', data.job); container.setAttribute('data-training', data.training); + container.setAttribute('data-report', data.report); // User info. var info = document.createElement('div'); @@ -147,6 +148,7 @@ // Rights. actions.appendChild(this.createSelect('job', data.job, disabled)); actions.appendChild(this.createSelect('training', data.training, disabled)); + actions.appendChild(this.createSelect('report', data.report, disabled)); // Remove. actions.appendChild(this.createButton('remove', t('Remove'), true, '', disabled)); @@ -205,6 +207,7 @@ container.setAttribute('data-filters', ''); container.setAttribute('data-job', 'all'); container.setAttribute('data-training', 'all'); + container.setAttribute('data-report', 'all'); var title = document.createElement('span'); title.appendChild(document.createTextNode(t('Filter: '))); @@ -213,6 +216,7 @@ // Rights filters. container.appendChild(this.createSelect('job', '', false, true)); container.appendChild(this.createSelect('training', '', false, true)); + container.appendChild(this.createSelect('report', '', false, true)); return container; }, @@ -418,6 +422,7 @@ id: element.getAttribute('data-id'), job: Math.max(element.querySelector('select[data-name="job"]').selectedIndex, 0), training: Math.max(element.querySelector('select[data-name="training"]').selectedIndex, 0), + report: Math.max(element.querySelector('select[data-name="report"]').selectedIndex, 0), notes: element.querySelector('textarea').value.trim() }; }, @@ -511,7 +516,7 @@ var name = target.getAttribute('data-name'); // Update the rights attributes of the user row. - if (name === 'job' || name === 'training') { + if (name === 'job' || name === 'training' || name === 'report') { var parent = target.parentNode.parentNode; // If the parent is not the filter container, then it's a select diff --git a/html/modules/custom/reliefweb_fields/reliefweb_fields.module b/html/modules/custom/reliefweb_fields/reliefweb_fields.module index 2eb0de199..5ffa526ad 100644 --- a/html/modules/custom/reliefweb_fields/reliefweb_fields.module +++ b/html/modules/custom/reliefweb_fields/reliefweb_fields.module @@ -5,7 +5,6 @@ * Module file for the ReliefWeb Fields module. */ -use Drupal\Core\Database\Database; use Drupal\Core\Entity\EntityInterface; use Drupal\filter\FilterFormatInterface; use Drupal\reliefweb_fields\Plugin\Field\FieldWidget\ReliefWebLinks; From ea48846da286b4921cbaccc79de55b90242a66d6 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 15:43:34 +0200 Subject: [PATCH 03/79] feat: Add report to user posting rights on people Refs: #RW-1058, #RW-1059 --- .../src/Controller/UserController.php | 12 +++++++++--- .../reliefweb_users/src/Form/UserPageFilterForm.php | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/html/modules/custom/reliefweb_users/src/Controller/UserController.php b/html/modules/custom/reliefweb_users/src/Controller/UserController.php index b7cdf9060..9b65705f9 100644 --- a/html/modules/custom/reliefweb_users/src/Controller/UserController.php +++ b/html/modules/custom/reliefweb_users/src/Controller/UserController.php @@ -127,7 +127,7 @@ protected function usersAdminList($storage, $query_parameters) { 'mail' => ['data' => $this->t('Mail'), 'field' => 'u.mail'], 'status' => ['data' => $this->t('Status'), 'field' => 'u.status'], 'role' => $this->t('Roles'), - 'sources' => $this->t('Sources (Job, Training)'), + 'sources' => $this->t('Sources (Job, Training, Reports)'), 'created' => ['data' => $this->t('Member for'), 'field' => 'u.created'], 'access' => ['data' => $this->t('Last access'), 'field' => 'u.access'], 'edit' => $this->t('Edit'), @@ -170,7 +170,7 @@ protected function usersAdminList($storage, $query_parameters) { } // Posting rights filter. - if (isset($filters['job_rights']) || isset($filters['training_rights'])) { + if (isset($filters['job_rights']) || isset($filters['training_rights']) || isset($filters['report_rights'])) { $query->innerJoin('taxonomy_term__field_user_posting_rights', 'fpr', '%alias.field_user_posting_rights_id = u.uid'); if (isset($filters['job_rights'])) { $query->condition('fpr.field_user_posting_rights_job', $rights[$filters['job_rights']], '='); @@ -178,6 +178,9 @@ protected function usersAdminList($storage, $query_parameters) { if (isset($filters['training_rights'])) { $query->condition('fpr.field_user_posting_rights_training', $rights[$filters['training_rights']], '='); } + if (isset($filters['report_rights'])) { + $query->condition('fpr.field_user_posting_rights_report', $rights[$filters['report_rights']], '='); + } } // Set group by. @@ -272,6 +275,7 @@ public function getUserSources(array &$users) { $query->addField('f', 'field_user_posting_rights_id', 'uid'); $query->addField('f', 'field_user_posting_rights_job', 'job'); $query->addField('f', 'field_user_posting_rights_training', 'training'); + $query->addField('f', 'field_user_posting_rights_report', 'report'); $query->addField('f', 'entity_id', 'tid'); $query->addExpression('COALESCE(fs.field_shortname_value, td.name)', 'name'); $query->condition('f.field_user_posting_rights_id', array_keys($users), 'IN'); @@ -281,10 +285,12 @@ public function getUserSources(array &$users) { foreach ($query->execute() as $record) { $job = $record->job; $training = $record->training; + $report = $record->report; + $link = Link::fromTextAndUrl($record->name, URL::fromUserInput('/taxonomy/term/' . $record->tid . '/user-posting-rights', [ 'attributes' => ['target' => '_blank'], ])); - $row = '
  • ' . $link->toString() . '
  • '; + $row = '
  • ' . $link->toString() . '
  • '; $sources[$record->uid][$record->tid] = $row; } diff --git a/html/modules/custom/reliefweb_users/src/Form/UserPageFilterForm.php b/html/modules/custom/reliefweb_users/src/Form/UserPageFilterForm.php index 1a66ff68f..12a2e28b5 100644 --- a/html/modules/custom/reliefweb_users/src/Form/UserPageFilterForm.php +++ b/html/modules/custom/reliefweb_users/src/Form/UserPageFilterForm.php @@ -113,6 +113,13 @@ public function buildForm(array $form, FormStateInterface $form_state, AccountIn '#default_value' => $filters['training_rights'] ?? 'any', ]; + $form['filters']['report_rights'] = [ + '#type' => 'radios', + '#title' => $this->t('Report posting rights'), + '#options' => ['any' => 'Any'] + $rights, + '#default_value' => $filters['report_rights'] ?? 'any', + ]; + $form['filters']['name'] = [ '#type' => 'textfield', '#title' => $this->t('Name'), @@ -197,6 +204,7 @@ protected function getFilters(FormStateInterface $form_state) { 'posted' => isset($inputs['posted']) && is_array($inputs['posted']) ? array_intersect_key($content_types, $inputs['posted']) : NULL, 'job_rights' => isset($inputs['job_rights'], $rights[$inputs['job_rights']]) ? $inputs['job_rights'] : NULL, 'training_rights' => isset($inputs['training_rights'], $rights[$inputs['training_rights']]) ? $inputs['training_rights'] : NULL, + 'report_rights' => isset($inputs['report_rights'], $rights[$inputs['report_rights']]) ? $inputs['report_rights'] : NULL, ]; } From f3e73c0a3a6e0b3a8169bf59bd0a4d48aec92369 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 16:00:15 +0200 Subject: [PATCH 04/79] feat: Allow anon to create reports Refs: #RW-1058, #RW-1060 --- config/user.role.authenticated.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/user.role.authenticated.yml b/config/user.role.authenticated.yml index 0ac05b79c..6d228dffb 100644 --- a/config/user.role.authenticated.yml +++ b/config/user.role.authenticated.yml @@ -7,6 +7,7 @@ dependencies: - filter.format.markdown_editor - filter.format.token_markdown - node.type.job + - node.type.report - node.type.training module: - content_entity_clone @@ -31,6 +32,7 @@ permissions: - 'bookmark content' - 'clone content entities' - 'create job content' + - 'create report content' - 'create training content' - 'delete own job content' - 'delete own training content' From 8d02df9752a17f92bdee7a315b4953afd01075f8 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 16:13:12 +0200 Subject: [PATCH 05/79] feat: Limit sources Refs: #RW-1058, #RW-1062 --- .../src/EntityFormAlterServiceBase.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php b/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php index f006e7ffd..7c8381f2e 100644 --- a/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php +++ b/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php @@ -384,8 +384,8 @@ protected function addPotentialNewSourceFields(array &$form, FormStateInterface $entity = $form_state->getFormObject()->getEntity(); $bundle = $entity->bundle(); - // The following only applies to job and training nodes. - if (!in_array($bundle, ['job', 'training'])) { + // The following only applies to job, training and report nodes. + if (!in_array($bundle, ['job', 'training', 'report'])) { return; } @@ -582,7 +582,7 @@ public static function retrievePotentialNewSourceInformation(EntityModeratedInte } /** - * Add the user information to a job/training node form. + * Add the user information to a job/training/report node form. * * @param array $form * The entity form. @@ -594,8 +594,8 @@ protected function addUserInformation(array &$form, FormStateInterface $form_sta $entity_id = $entity->id(); $bundle = $entity->bundle(); - // It's only for jobs and training. - if (!in_array($bundle, ['job', 'training'])) { + // It's only for jobs, trainings and reports. + if (!in_array($bundle, ['job', 'training', 'report'])) { return; } From c1bc848a6fa037b94d289b5370951cb466a45f62 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 16:26:45 +0200 Subject: [PATCH 06/79] feat: My posts Refs: #RW-1058, #RW-1063 --- .../reliefweb_moderation/src/ModerationServiceBase.php | 4 ++-- .../src/Form/UserPostsPageFilterForm.php | 5 ++++- .../src/Services/UserPostsService.php | 10 +++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_moderation/src/ModerationServiceBase.php b/html/modules/custom/reliefweb_moderation/src/ModerationServiceBase.php index f056f74b4..0023ca00c 100644 --- a/html/modules/custom/reliefweb_moderation/src/ModerationServiceBase.php +++ b/html/modules/custom/reliefweb_moderation/src/ModerationServiceBase.php @@ -1913,8 +1913,8 @@ protected function joinPostingRights(Select $query, array $definition, $entity_t return ''; } - // This is only valid for jobs and training. Skip it otherwise. - if ($bundle !== 'job' && $bundle !== 'training') { + // This is only valid for jobs, trainings and reports. Skip it otherwise. + if ($bundle !== 'job' && $bundle !== 'training' && $bundle !== 'report') { return ''; } diff --git a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php index 8f88bc15b..e6ab6fa17 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php +++ b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php @@ -30,13 +30,16 @@ public function buildForm(array $form, FormStateInterface $form_state, Moderatio // Link to create a new entity. $url_options = ['attributes' => ['target' => '_blank']]; - $links = $this->t('Create a new Job vacancy or a new Training program', [ + $links = $this->t('Create a new Job vacancy, a new Training program or a new Report', [ '@job_url' => Url::fromRoute('node.add', [ 'node_type' => 'job', ], $url_options)->toString(), '@training_url' => Url::fromRoute('node.add', [ 'node_type' => 'training', ], $url_options)->toString(), + '@report_url' => Url::fromRoute('node.add', [ + 'node_type' => 'report', + ], $url_options)->toString(), ]); // Add intro. diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index 32b11619b..1300e99e2 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -87,6 +87,7 @@ public function getBundle() { return [ 'job', 'training', + 'report', ]; } @@ -229,7 +230,7 @@ public function getRows(array $results) { $cells['deadline'] = $this->formatDate($entity->field_registration_deadline->value); } } - else { + elseif ($entity->bundle() === 'job') { $cells['deadline'] = $this->formatDate($entity->field_job_closing_date->value); } @@ -271,6 +272,7 @@ protected function initFilterDefinitions(array $filters = []) { 'values' => [ 'job' => $this->t('Job'), 'training' => $this->t('Training'), + 'report' => $this->t('Report'), ], ]; @@ -356,6 +358,9 @@ protected function getSourcesTheUserHasPostedFor($filter, $term, $conditions, ar elseif (isset($rights[$source->value]['training']) && $rights[$source->value]['training'] > $min_right) { $allowed_sources[] = $source; } + elseif (isset($rights[$source->value]['report']) && $rights[$source->value]['report'] > $min_right) { + $allowed_sources[] = $source; + } } } @@ -462,6 +467,9 @@ protected function filterQuery(Select $query, array $filters = []) { if (empty($filters['bundle']) || !empty($filters['bundle']['training'])) { $types[] = 'training'; } + if (empty($filters['bundle']) || !empty($filters['bundle']['report'])) { + $types[] = 'report'; + } // Get the user rights keyed by source ids and store the ones // for which the user is allowed to post. From c06c1a1aee831f3b9f701ac9e5fe12c45521705b Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 16:31:03 +0200 Subject: [PATCH 07/79] feat: Moderate reports Refs: #RW-1058, #RW-1064 --- .../reliefweb_moderation/src/Services/ReportModeration.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 50d214e32..0e3923b8d 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -7,6 +7,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\reliefweb_moderation\EntityModeratedInterface; +use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; use Drupal\reliefweb_moderation\ModerationServiceBase; use Drupal\reliefweb_utility\Helpers\UserHelper; @@ -109,6 +110,8 @@ public function getRows(array $results) { // Country and source info. $info = []; + // User posting rights. + $info['posting_rights'] = UserPostingRightsHelper::renderRight(UserPostingRightsHelper::getEntityAuthorPostingRights($entity)); // Country. $country_link = $this->getTaxonomyTermLink($entity->field_primary_country->first()); if (!empty($country_link)) { @@ -316,6 +319,7 @@ protected function initFilterDefinitions(array $filters = []) { 'original_publication_date', 'author', 'user_role', + 'posting_rights', 'reviewer', 'reviewed', 'comments', From b2b1a686dc5a979c1926341ee95da860200d7436 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 19 Sep 2024 16:35:03 +0200 Subject: [PATCH 08/79] feat: Report submission Refs: #RW-1058, #RW-1066 --- html/modules/custom/reliefweb_entities/src/Entity/Report.php | 2 ++ .../custom/reliefweb_entities/src/OpportunityDocumentTrait.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index df079fee3..92f3e9dc4 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -11,6 +11,7 @@ use Drupal\reliefweb_entities\BundleEntityInterface; use Drupal\reliefweb_entities\DocumentInterface; use Drupal\reliefweb_entities\DocumentTrait; +use Drupal\reliefweb_entities\OpportunityDocumentTrait; use Drupal\reliefweb_moderation\EntityModeratedInterface; use Drupal\reliefweb_moderation\EntityModeratedTrait; use Drupal\reliefweb_revisions\EntityRevisionedInterface; @@ -27,6 +28,7 @@ class Report extends Node implements BundleEntityInterface, EntityModeratedInter use DocumentTrait; use EntityModeratedTrait; use EntityRevisionedTrait; + use OpportunityDocumentTrait; use StringTranslationTrait; /** diff --git a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php index 44ed15aa7..b303bb768 100644 --- a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php @@ -9,7 +9,7 @@ use Drupal\reliefweb_utility\Helpers\UserHelper; /** - * Trait for "opportunity" documents like jobs and training. + * Trait for "opportunity" documents like jobs, trainings and reports. * * @see Drupal\reliefweb_entities\DocuemntInterface */ From 0f1866dccd916311a6f53933a3a010a45305f100 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 20 Sep 2024 10:05:49 +0200 Subject: [PATCH 09/79] feat: Source revision log Refs: #RW-1058, #RW-1065 --- .../reliefweb_revisions/src/Services/EntityHistory.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/html/modules/custom/reliefweb_revisions/src/Services/EntityHistory.php b/html/modules/custom/reliefweb_revisions/src/Services/EntityHistory.php index 44ef43965..4884ec104 100644 --- a/html/modules/custom/reliefweb_revisions/src/Services/EntityHistory.php +++ b/html/modules/custom/reliefweb_revisions/src/Services/EntityHistory.php @@ -927,6 +927,7 @@ protected function formatReliefWebUserPostingRightsFieldDiff(FieldDefinitionInte 'removed' => array_diff_key($previous, $current), 'modified-training' => [], 'modified-job' => [], + 'modified-report' => [], 'modified-notes' => [], ]; @@ -935,6 +936,7 @@ protected function formatReliefWebUserPostingRightsFieldDiff(FieldDefinitionInte 'removed' => $this->t('Removed'), 'modified-training' => $this->t('Modified Training'), 'modified-job' => $this->t('Modified Job'), + 'modified-report' => $this->t('Modified Report'), 'modified-notes' => $this->t('Modified Notes'), ]; @@ -951,7 +953,7 @@ protected function formatReliefWebUserPostingRightsFieldDiff(FieldDefinitionInte $previous_item = $previous[$key]; $current_item = $current[$key]; // Rights change. - foreach (['job', 'training'] as $type) { + foreach (['job', 'training', 'report'] as $type) { if ($previous_item[$type] !== $current_item[$type]) { $item['change'] = new FormattableMarkup('@before → @after', [ '@before' => UserPostingRightsHelper::renderRight($rights[$previous_item[$type]]), @@ -995,9 +997,10 @@ protected function formatReliefWebUserPostingRightsFieldDiff(FieldDefinitionInte // Add the rights when a user is added. if ($category === 'added') { - $markup[] = '(job: @job, training: @training)'; + $markup[] = '(job: @job, training: @training, report: @report)'; $replacements['@job'] = UserPostingRightsHelper::renderRight($rights[$item['job']]); $replacements['@training'] = UserPostingRightsHelper::renderRight($rights[$item['training']]); + $replacements['@report'] = UserPostingRightsHelper::renderRight($rights[$item['report']]); } // Add the rights changes. From a975f0e399339b9725a4a92f6709469654b3797c Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 20 Sep 2024 10:06:53 +0200 Subject: [PATCH 10/79] feat: Form actions for report Refs: #RW-1058, #RW-1066 --- .../reliefweb_entities/src/Entity/Report.php | 10 +- .../src/Services/ReportModeration.php | 91 +++++++++++++++---- 2 files changed, 79 insertions(+), 22 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 92f3e9dc4..7758e15e2 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -205,8 +205,6 @@ public function getAttachments(?array $build = NULL) { * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { - parent::preSave($storage); - // Change the publication date if bury is selected, to the original // publication date. if (!empty($this->field_bury->value) && !$this->field_original_publication_date->isEmpty()) { @@ -251,6 +249,14 @@ public function preSave(EntityStorageInterface $storage) { // Prepare notifications. $this->preparePublicationNotification(); + + // Update the entity status based on the user posting rights. + $this->updateModerationStatusFromPostingRights(); + + // Update the entity status based on the source(s) moderation status. + $this->updateModerationStatusFromSourceStatus(); + + parent::preSave($storage); } /** diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 0e3923b8d..035789ca6 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -224,30 +224,81 @@ public function getFilterDefaultStatuses() { * {@inheritdoc} */ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $entity) { - $buttons = [ - 'draft' => [ + $buttons = []; + $new = empty($status) || $status === 'draft' || $entity->isNew(); + + // Only show save as draft for non-published but editable documents. + if ($new || in_array($status, ['draft', 'pending', 'on-hold'])) { + $buttons['draft'] = [ '#value' => $this->t('Save as draft'), - ], - 'to-review' => [ - '#value' => $this->t('To review'), - ], - 'published' => [ - '#value' => $this->t('Publish'), - ], - 'on-hold' => [ - '#value' => $this->t('On-hold'), - ], - 'reference' => [ - '#value' => $this->t('Reference'), - ], - ]; + ]; + } - // @todo replace with permission. - if (UserHelper::userHasRoles(['administrator', 'webmaster'])) { - $buttons['archive'] = [ - '#value' => $this->t('Archive'), + // Editors can publish, put on hold or refuse a document. + // @todo use permission. + if (UserHelper::userHasRoles(['editor'])) { + $buttons = [ + 'draft' => [ + '#value' => $this->t('Save as draft'), + ], + 'to-review' => [ + '#value' => $this->t('To review'), + ], + 'published' => [ + '#value' => $this->t('Publish'), + ], + 'on-hold' => [ + '#value' => $this->t('On-hold'), + ], + 'reference' => [ + '#value' => $this->t('Reference'), + ], ]; } + elseif (UserHelper::userHasRoles(['administrator', 'webmaster'])) { + $buttons = [ + 'draft' => [ + '#value' => $this->t('Save as draft'), + ], + 'to-review' => [ + '#value' => $this->t('To review'), + ], + 'published' => [ + '#value' => $this->t('Publish'), + ], + 'on-hold' => [ + '#value' => $this->t('On-hold'), + ], + 'reference' => [ + '#value' => $this->t('Reference'), + ], + 'archive' => [ + '#value' => $this->t('Archive'), + ], + ]; + } + // Other users can submit for review (or publish directly if trusted). + else { + $buttons['pending'] = [ + '#value' => $new ? $this->t('Submit') : $this->t('Submit changes'), + ]; + + // Add confirmation when attempting to change published document. + if ($status === 'published' || $status === 'expired') { + $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The job may be set as pending.'); + $buttons['pending']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; + } + } + + // Warning message when saving as a draft. + if (isset($buttons['draft'])) { + $message = $this->t('You are saving this document as a draft. It will not be visible to visitors. If you wish to proceed with the publication kindly click on @buttons instead.', [ + '@buttons' => implode(' or ', array_map(function ($item) { + return $item['#value']; + }, array_slice($buttons, 1))), + ]); + $buttons['draft']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; + } return $buttons; } From 329569f161117fb0299ad1b1a8ccc90aed4e0469 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 20 Sep 2024 10:07:16 +0200 Subject: [PATCH 11/79] feat: Source revision log Refs: #RW-1058, #RW-1065 --- .../reliefweb_moderation/src/Services/SourceModeration.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/SourceModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/SourceModeration.php index d8b83b87b..5be9d3d39 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/SourceModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/SourceModeration.php @@ -225,9 +225,10 @@ public function entityPresave(EntityModeratedInterface $entity) { // Update the posting rights field, setting everything as blocked. if (!$entity->get('field_user_posting_rights')->isEmpty()) { foreach ($entity->get('field_user_posting_rights') as $item) { - if ($item->get('job')->getValue() != 1 || $item->get('training')->getValue() != 1) { + if ($item->get('job')->getValue() != 1 || $item->get('training')->getValue() != 1 || $item->get('report')->getValue() != 1) { $item->get('job')->setValue(1); $item->get('training')->setValue(1); + $item->get('report')->setValue(1); $changed = TRUE; } } From feee802fe28feb05120c84fbff22e3d0594dbf35 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 26 Sep 2024 09:41:11 +0200 Subject: [PATCH 12/79] feat: Add contributor role Refs: #RW-1058 --- ...ction.user_add_role_action.contributor_role.yml | 14 ++++++++++++++ ...on.user_remove_role_action.contributor_role.yml | 14 ++++++++++++++ config/user.role.authenticated.yml | 2 -- config/user.role.contributor_role.yml | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 config/system.action.user_add_role_action.contributor_role.yml create mode 100644 config/system.action.user_remove_role_action.contributor_role.yml create mode 100644 config/user.role.contributor_role.yml diff --git a/config/system.action.user_add_role_action.contributor_role.yml b/config/system.action.user_add_role_action.contributor_role.yml new file mode 100644 index 000000000..b5106e9f5 --- /dev/null +++ b/config/system.action.user_add_role_action.contributor_role.yml @@ -0,0 +1,14 @@ +uuid: 8a1c6d2a-cf03-48d7-9dc8-ba12b7239159 +langcode: en +status: true +dependencies: + config: + - user.role.contributor_role + module: + - user +id: user_add_role_action.contributor_role +label: 'Add the Contributor role role to the selected user(s)' +type: user +plugin: user_add_role_action +configuration: + rid: contributor_role diff --git a/config/system.action.user_remove_role_action.contributor_role.yml b/config/system.action.user_remove_role_action.contributor_role.yml new file mode 100644 index 000000000..ea5689c22 --- /dev/null +++ b/config/system.action.user_remove_role_action.contributor_role.yml @@ -0,0 +1,14 @@ +uuid: e0c6f28b-74a1-41a4-a75f-b9c12fe98676 +langcode: en +status: true +dependencies: + config: + - user.role.contributor_role + module: + - user +id: user_remove_role_action.contributor_role +label: 'Remove the Contributor role role from the selected user(s)' +type: user +plugin: user_remove_role_action +configuration: + rid: contributor_role diff --git a/config/user.role.authenticated.yml b/config/user.role.authenticated.yml index 131133bd4..7556166b6 100644 --- a/config/user.role.authenticated.yml +++ b/config/user.role.authenticated.yml @@ -7,7 +7,6 @@ dependencies: - filter.format.markdown_editor - filter.format.token_markdown - node.type.job - - node.type.report - node.type.training module: - content_entity_clone @@ -34,7 +33,6 @@ permissions: - 'bookmark content' - 'clone content entities' - 'create job content' - - 'create report content' - 'create training content' - 'delete own job content' - 'delete own training content' diff --git a/config/user.role.contributor_role.yml b/config/user.role.contributor_role.yml new file mode 100644 index 000000000..a9c73ae4d --- /dev/null +++ b/config/user.role.contributor_role.yml @@ -0,0 +1,14 @@ +uuid: 42dacb5f-f71a-4e37-858e-d4cf46729e39 +langcode: en +status: true +dependencies: + config: + - node.type.report + module: + - node +id: contributor_role +label: 'Contributor role' +weight: 9 +is_admin: null +permissions: + - 'create report content' From e1e00896c833ec31e51f7cb7350253946bbce360 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 26 Sep 2024 13:08:23 +0200 Subject: [PATCH 13/79] chore: Small fixes --- .../src/Services/ReportModeration.php | 22 ++++++------ .../src/Form/UserPostsPageFilterForm.php | 34 +++++++++++++------ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 035789ca6..3d29579ee 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -284,20 +284,22 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en ]; // Add confirmation when attempting to change published document. - if ($status === 'published' || $status === 'expired') { - $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The job may be set as pending.'); + if ($status === 'published') { + $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The report may be set as pending.'); $buttons['pending']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; } } - // Warning message when saving as a draft. - if (isset($buttons['draft'])) { - $message = $this->t('You are saving this document as a draft. It will not be visible to visitors. If you wish to proceed with the publication kindly click on @buttons instead.', [ - '@buttons' => implode(' or ', array_map(function ($item) { - return $item['#value']; - }, array_slice($buttons, 1))), - ]); - $buttons['draft']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; + if (UserHelper::userHasRoles(['contributor_role'])) { + // Warning message when saving as a draft. + if (isset($buttons['draft'])) { + $message = $this->t('You are saving this document as a draft. It will not be visible to visitors. If you wish to proceed with the publication kindly click on @buttons instead.', [ + '@buttons' => implode(' or ', array_map(function ($item) { + return $item['#value']; + }, array_slice($buttons, 1))), + ]); + $buttons['draft']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; + } } return $buttons; diff --git a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php index e6ab6fa17..03fa76591 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php +++ b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php @@ -30,17 +30,29 @@ public function buildForm(array $form, FormStateInterface $form_state, Moderatio // Link to create a new entity. $url_options = ['attributes' => ['target' => '_blank']]; - $links = $this->t('Create a new Job vacancy, a new Training program or a new Report', [ - '@job_url' => Url::fromRoute('node.add', [ - 'node_type' => 'job', - ], $url_options)->toString(), - '@training_url' => Url::fromRoute('node.add', [ - 'node_type' => 'training', - ], $url_options)->toString(), - '@report_url' => Url::fromRoute('node.add', [ - 'node_type' => 'report', - ], $url_options)->toString(), - ]); + if ($user && $user->hasRole('contributor_role')) { + $links = $this->t('Create a new Job vacancy, a new Training program or a new Report', [ + '@job_url' => Url::fromRoute('node.add', [ + 'node_type' => 'job', + ], $url_options)->toString(), + '@training_url' => Url::fromRoute('node.add', [ + 'node_type' => 'training', + ], $url_options)->toString(), + '@report_url' => Url::fromRoute('node.add', [ + 'node_type' => 'report', + ], $url_options)->toString(), + ]); + } + else { + $links = $this->t('Create a new Job vacancy or a new Training program', [ + '@job_url' => Url::fromRoute('node.add', [ + 'node_type' => 'job', + ], $url_options)->toString(), + '@training_url' => Url::fromRoute('node.add', [ + 'node_type' => 'training', + ], $url_options)->toString(), + ]); + } // Add intro. $form['intro'] = [ From 467532239d362a7b5e23177355dfce344859c0a7 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 26 Sep 2024 13:34:24 +0200 Subject: [PATCH 14/79] chore: Add pending state --- .../reliefweb_moderation/src/Services/ReportModeration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 3d29579ee..1d4d7df86 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -206,6 +206,7 @@ public function getStatuses() { 'to-review' => $this->t('To review'), 'published' => $this->t('Published'), 'embargoed' => $this->t('Embargoed'), + 'pending' => $this->t('Pending'), 'archive' => $this->t('Archived'), 'reference' => $this->t('Reference'), ]; From f9ba5083291e8c0be832cb0dfbb106b0a88b9281 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 26 Sep 2024 21:03:43 +0200 Subject: [PATCH 15/79] chore: no more pending --- .../src/Services/ReportModeration.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 1d4d7df86..a8358274e 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -206,7 +206,6 @@ public function getStatuses() { 'to-review' => $this->t('To review'), 'published' => $this->t('Published'), 'embargoed' => $this->t('Embargoed'), - 'pending' => $this->t('Pending'), 'archive' => $this->t('Archived'), 'reference' => $this->t('Reference'), ]; @@ -229,7 +228,7 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en $new = empty($status) || $status === 'draft' || $entity->isNew(); // Only show save as draft for non-published but editable documents. - if ($new || in_array($status, ['draft', 'pending', 'on-hold'])) { + if ($new || in_array($status, ['draft', 'on-hold'])) { $buttons['draft'] = [ '#value' => $this->t('Save as draft'), ]; @@ -280,14 +279,14 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en } // Other users can submit for review (or publish directly if trusted). else { - $buttons['pending'] = [ + $buttons['to-review'] = [ '#value' => $new ? $this->t('Submit') : $this->t('Submit changes'), ]; // Add confirmation when attempting to change published document. if ($status === 'published') { - $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The report may be set as pending.'); - $buttons['pending']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; + $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The report may be set as to-review.'); + $buttons['to-review']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; } } From 0d7e02eb4c36e893f56abc6b2fe77a40457b187a Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 10:33:49 +0200 Subject: [PATCH 16/79] test: Basic report test --- .../tests/src/ExistingSite/RwReportTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php new file mode 100644 index 000000000..306a06eef --- /dev/null +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php @@ -0,0 +1,58 @@ +getViewBuilder($entity_type); + $build = $view_builder->view($entity); + $output = \Drupal::service('renderer')->renderRoot($build); + + return $output->__toString(); + } + + /** + * Test report. + */ + public function testReport() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = 'My report'; + + $report = Node::create([ + 'type' => 'report', + 'title' => $title, + 'field_origin' => 0, + 'field_origin_notes' => 'https://www.example.com/my-report', + ]); + + // Report will be saved as draft. + $report->setPublished()->save(); + + // 404 for anonymous. + $this->drupalGet($report->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // OK for admins. + $admin = User::load(1); + $this->drupalLogin($admin); + + $this->drupalGet($report->toUrl()); + $this->assertSession()->titleEquals($title . ' | ' . $site_name); + $this->assertSession()->elementTextEquals('css', '.rw-article__title.rw-page-title', $title); + } + +} From 787ab8879217971da47a5947531d54117443031a Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 11:13:48 +0200 Subject: [PATCH 17/79] test: Basic report test --- .../src/ExistingSite/RwReportAddTest.php | 50 +++++++++++++++++++ ...wReportTest.php => RwReportCreateTest.php} | 16 +----- 2 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php rename html/modules/custom/reliefweb_entities/tests/src/ExistingSite/{RwReportTest.php => RwReportCreateTest.php} (67%) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php new file mode 100644 index 000000000..c7f2d2e67 --- /dev/null +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -0,0 +1,50 @@ +get('name'); + $title = $this->randomMachineName(8); + + $admin = User::load(1); + $this->drupalLogin($admin); + + // Create a node. + $edit = []; + $edit['title[0][value]'] = $title; + $edit['field_language[267]'] = 'en'; + $edit['field_country[]'] = ['34']; + $edit['field_primary_country'] = '34'; + $edit['field_content_format'] = '11'; + $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; + $edit['field_source[]'] = ['43679']; + $edit['field_source[]'] = ['43679']; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Publish'); + + // Check that the Basic page has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Published'); + } + +} diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php similarity index 67% rename from html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php rename to html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index 306a06eef..0cf3c3940 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -4,26 +4,14 @@ namespace Drupal\Tests\reliefweb_entities\ExistingSite; -use Drupal\Core\Url; -use Drupal\group\Entity\Group; use Drupal\node\Entity\Node; -use Drupal\paragraphs\Entity\Paragraph; -use Drupal\theme_switcher\Entity\ThemeSwitcherRule; use Drupal\user\Entity\User; use weitzman\DrupalTestTraits\ExistingSiteBase; /** - * Tests sidebar feed. + * Tests reports. */ -class RwReportTest extends ExistingSiteBase { - - protected function renderIt($entity_type, $entity) { - $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type); - $build = $view_builder->view($entity); - $output = \Drupal::service('renderer')->renderRoot($build); - - return $output->__toString(); - } +class RwReportCreateTest extends ExistingSiteBase { /** * Test report. From e4ef535dce4e736933321e34232e2c16dda7da8b Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 13:50:36 +0200 Subject: [PATCH 18/79] test: Create terms if needed --- .../src/ExistingSite/RwReportAddTest.php | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index c7f2d2e67..0768b4862 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -5,6 +5,7 @@ namespace Drupal\Tests\reliefweb_entities\ExistingSite; use Drupal\node\Entity\Node; +use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\User; use weitzman\DrupalTestTraits\ExistingSiteBase; @@ -23,16 +24,20 @@ public function testAddReportAsAdminPublished() { $admin = User::load(1); $this->drupalLogin($admin); + $term_language = $this->createTermIfNeeded('language', 267, 'English'); + $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); + $term_format = $this->createTermIfNeeded('content_format', 267, 'UN Document'); + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color'); + // Create a node. $edit = []; $edit['title[0][value]'] = $title; - $edit['field_language[267]'] = 'en'; - $edit['field_country[]'] = ['34']; - $edit['field_primary_country'] = '34'; - $edit['field_content_format'] = '11'; + $edit['field_language[' . $term_language->id() . ']'] = $term_language->id(); + $edit['field_country[]'] = [$term_country->id()]; + $edit['field_primary_country'] = $term_country->id(); + $edit['field_content_format'] = $term_format->id(); $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; - $edit['field_source[]'] = ['43679']; - $edit['field_source[]'] = ['43679']; + $edit['field_source[]'] = [$term_source->id()]; $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Publish'); @@ -47,4 +52,20 @@ public function testAddReportAsAdminPublished() { $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Published'); } + /** + * Create terms. + */ + protected function createTermIfNeeded($vocabulary, $id, $title) : Term { + if ($term = Term::load($id)) { + return $term; + } + + $term = Term::create([ + 'vid' => $vocabulary, + 'name' => $title, + 'id' => $id, + ]); + $term->save(); + return $term; + } } From 2c0c602b9ba7fd00c3f04051406eb9728d073ef1 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 15:00:16 +0200 Subject: [PATCH 19/79] test: Create terms if needed --- .../tests/src/ExistingSite/RwReportAddTest.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 0768b4862..7f4a9c922 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -27,7 +27,11 @@ public function testAddReportAsAdminPublished() { $term_language = $this->createTermIfNeeded('language', 267, 'English'); $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); $term_format = $this->createTermIfNeeded('content_format', 267, 'UN Document'); - $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color'); + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); // Create a node. $edit = []; @@ -55,7 +59,7 @@ public function testAddReportAsAdminPublished() { /** * Create terms. */ - protected function createTermIfNeeded($vocabulary, $id, $title) : Term { + protected function createTermIfNeeded($vocabulary, $id, $title, array $extra = []) : Term { if ($term = Term::load($id)) { return $term; } @@ -64,7 +68,7 @@ protected function createTermIfNeeded($vocabulary, $id, $title) : Term { 'vid' => $vocabulary, 'name' => $title, 'id' => $id, - ]); + ] + $extra); $term->save(); return $term; } From 34a141b16ad518b0ea8d570bb88feb8d4711cea4 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 15:13:25 +0200 Subject: [PATCH 20/79] test: Basic report test --- .../src/ExistingSite/RwReportAddTest.php | 75 +++++++++++-------- .../tests/src/ExistingSite/RwReportBase.php | 49 ++++++++++++ 2 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 7f4a9c922..6354c156d 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -4,15 +4,12 @@ namespace Drupal\Tests\reliefweb_entities\ExistingSite; -use Drupal\node\Entity\Node; -use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\User; -use weitzman\DrupalTestTraits\ExistingSiteBase; /** * Add a report using browser. */ -class RwReportAddTest extends ExistingSiteBase { +class RwReportAddTest extends RwReportBase { /** * Test adding a report as admin, published. @@ -24,9 +21,48 @@ public function testAddReportAsAdminPublished() { $admin = User::load(1); $this->drupalLogin($admin); + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Publish'); + + // Check that the Basic page has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Published'); + } + + /** + * Test adding a report as admin, draft. + */ + public function testAddReportAsAdminDraft() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(8); + + $admin = User::load(1); + $this->drupalLogin($admin); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Save as draft'); + + // Check that the Basic page has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Draft'); + } + + protected function getEditFields($title) { $term_language = $this->createTermIfNeeded('language', 267, 'English'); $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); - $term_format = $this->createTermIfNeeded('content_format', 267, 'UN Document'); + $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ 'field_allowed_content_types' => [ 1, @@ -43,33 +79,6 @@ public function testAddReportAsAdminPublished() { $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; $edit['field_source[]'] = [$term_source->id()]; - $this->drupalGet('node/add/report'); - $this->submitForm($edit, 'Publish'); - - // Check that the Basic page has been created. - $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); - $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); - $this->assertSession()->pageTextContains('Belgium'); - $this->assertSession()->pageTextContains('ABC Color'); - $this->assertSession()->pageTextContains('UN Document'); - $this->assertSession()->pageTextContains('English'); - $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Published'); - } - - /** - * Create terms. - */ - protected function createTermIfNeeded($vocabulary, $id, $title, array $extra = []) : Term { - if ($term = Term::load($id)) { - return $term; - } - - $term = Term::create([ - 'vid' => $vocabulary, - 'name' => $title, - 'id' => $id, - ] + $extra); - $term->save(); - return $term; + return $edit; } } diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php new file mode 100644 index 000000000..2071a6aeb --- /dev/null +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -0,0 +1,49 @@ + $vocabulary, + 'name' => $title, + 'id' => $id, + ] + $extra); + $term->save(); + return $term; + } + + /** + * Create user if needed. + */ + protected function createUserIfNeeded($id, $name, array $extra = []) : User { + if ($user = User::load($id)) { + return $user; + } + + $user = User::create([ + 'name' => $name, + 'id' => $id, + ] + $extra); + $user->save(); + return $user; + } +} From b48a5eb3817906b80bc0f922ed7c35c2658ba9e9 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 15:53:43 +0200 Subject: [PATCH 21/79] test: Basic report test for contributor --- .../src/ExistingSite/RwReportAddTest.php | 124 +++++++++++++++++- .../tests/src/ExistingSite/RwReportBase.php | 1 + 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 6354c156d..bae2cbe7c 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -16,7 +16,7 @@ class RwReportAddTest extends RwReportBase { */ public function testAddReportAsAdminPublished() { $site_name = \Drupal::config('system.site')->get('name'); - $title = $this->randomMachineName(8); + $title = $this->randomMachineName(32); $admin = User::load(1); $this->drupalLogin($admin); @@ -25,7 +25,7 @@ public function testAddReportAsAdminPublished() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Publish'); - // Check that the Basic page has been created. + // Check that the report has been created. $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); $this->assertSession()->pageTextContains('Belgium'); @@ -33,6 +33,12 @@ public function testAddReportAsAdminPublished() { $this->assertSession()->pageTextContains('UN Document'); $this->assertSession()->pageTextContains('English'); $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Published'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); } /** @@ -40,7 +46,7 @@ public function testAddReportAsAdminPublished() { */ public function testAddReportAsAdminDraft() { $site_name = \Drupal::config('system.site')->get('name'); - $title = $this->randomMachineName(8); + $title = $this->randomMachineName(32); $admin = User::load(1); $this->drupalLogin($admin); @@ -49,7 +55,7 @@ public function testAddReportAsAdminDraft() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Save as draft'); - // Check that the Basic page has been created. + // Check that the report has been created. $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); $this->assertSession()->pageTextContains('Belgium'); @@ -57,6 +63,116 @@ public function testAddReportAsAdminDraft() { $this->assertSession()->pageTextContains('UN Document'); $this->assertSession()->pageTextContains('English'); $this->assertSession()->elementTextEquals('css', '.rw-moderation-information__status.rw-moderation-status', 'Draft'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + } + + /** + * Test adding a report as contributor, draft, allowed. + */ + public function testAddReportAsContributorDraftAllowed() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report allowed'); + if (!$user->hasRole('contributor_role')) { + $user->addRole('contributor_role'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '2', // Allowed. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Save as draft'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + } + + /** + * Test adding a report as contributor, draft, trusted. + */ + public function testAddReportAsContributorDraftTrusted() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report trusted'); + if (!$user->hasRole('contributor_role')) { + $user->addRole('contributor_role'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '3', // Trusted. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); } protected function getEditFields($title) { diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php index 2071a6aeb..0e15f47c3 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -46,4 +46,5 @@ protected function createUserIfNeeded($id, $name, array $extra = []) : User { $user->save(); return $user; } + } From 929290dfb743fccbce3b97089fa258651bc34d0d Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 16:06:17 +0200 Subject: [PATCH 22/79] test: Basic report test for contributor --- .../reliefweb_entities/tests/src/ExistingSite/RwReportBase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php index 0e15f47c3..85dbc61ef 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -42,6 +42,7 @@ protected function createUserIfNeeded($id, $name, array $extra = []) : User { $user = User::create([ 'name' => $name, 'id' => $id, + 'mail' => $this->randomMachineName(32) . '@localhost.localdomain', ] + $extra); $user->save(); return $user; From 3de412c39f36c7af50ec475b62b970f27028a2dd Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 16:22:54 +0200 Subject: [PATCH 23/79] test: Basic report test for contributor --- .../reliefweb_entities/tests/src/ExistingSite/RwReportBase.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php index 85dbc61ef..fbae60400 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -40,9 +40,10 @@ protected function createUserIfNeeded($id, $name, array $extra = []) : User { } $user = User::create([ + 'uid' => $id, 'name' => $name, - 'id' => $id, 'mail' => $this->randomMachineName(32) . '@localhost.localdomain', + 'status' => 1, ] + $extra); $user->save(); return $user; From ec035e00532bc1a535d06e6ae3f7d3e773ce1578 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 30 Sep 2024 16:36:54 +0200 Subject: [PATCH 24/79] test: Basic report test for contributor --- .../tests/src/ExistingSite/RwReportBase.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php index fbae60400..905bdb5e7 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportBase.php @@ -24,10 +24,11 @@ protected function createTermIfNeeded($vocabulary, $id, $title, array $extra = [ $term = Term::create([ 'vid' => $vocabulary, + 'tid' => $id, 'name' => $title, - 'id' => $id, ] + $extra); $term->save(); + return $term; } @@ -46,6 +47,7 @@ protected function createUserIfNeeded($id, $name, array $extra = []) : User { 'status' => 1, ] + $extra); $user->save(); + return $user; } From 52e8d617be268e70f96fd66c5c379222d0d5cd87 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 08:47:24 +0200 Subject: [PATCH 25/79] test: Basic report test for contributor --- .../src/ExistingSite/RwReportAddTest.php | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index bae2cbe7c..5cb69c48c 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -121,6 +121,64 @@ public function testAddReportAsContributorDraftAllowed() { $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'draft'); + } + + /** + * Test adding a report as contributor, submit, allowed. + */ + public function testAddReportAsContributorSubmitAllowed() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report allowed'); + if (!$user->hasRole('contributor_role')) { + $user->addRole('contributor_role'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '2', // Allowed. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); } /** @@ -156,6 +214,61 @@ public function testAddReportAsContributorDraftTrusted() { ]); $term_source->save(); + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Save as draft'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'draft'); + } + + /** + * Test adding a report as contributor, submit, trusted. + */ + public function testAddReportAsContributorSubmitTrusted() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report trusted'); + if (!$user->hasRole('contributor_role')) { + $user->addRole('contributor_role'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '3', // Trusted. + 'notes' => '', + ], + ]); + $term_source->save(); + $edit = $this->getEditFields($title); $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Submit'); @@ -173,6 +286,9 @@ public function testAddReportAsContributorDraftTrusted() { $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); } protected function getEditFields($title) { From 5afe818867ee9aa889c8d3191f94bc7cdc7d41fe Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 10:51:29 +0200 Subject: [PATCH 26/79] chore: Rename role --- ....action.user_add_role_action.contributor.yml} | 6 +++--- ...tion.user_remove_role_action.contributor.yml} | 6 +++--- ...ibutor_role.yml => user.role.contributor.yml} | 4 ++-- .../tests/src/ExistingSite/RwReportAddTest.php | 16 ++++++++-------- .../src/Services/ReportModeration.php | 2 +- .../src/Form/UserPostsPageFilterForm.php | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) rename config/{system.action.user_add_role_action.contributor_role.yml => system.action.user_add_role_action.contributor.yml} (70%) rename config/{system.action.user_remove_role_action.contributor_role.yml => system.action.user_remove_role_action.contributor.yml} (70%) rename config/{user.role.contributor_role.yml => user.role.contributor.yml} (81%) diff --git a/config/system.action.user_add_role_action.contributor_role.yml b/config/system.action.user_add_role_action.contributor.yml similarity index 70% rename from config/system.action.user_add_role_action.contributor_role.yml rename to config/system.action.user_add_role_action.contributor.yml index b5106e9f5..40e9e3b35 100644 --- a/config/system.action.user_add_role_action.contributor_role.yml +++ b/config/system.action.user_add_role_action.contributor.yml @@ -3,12 +3,12 @@ langcode: en status: true dependencies: config: - - user.role.contributor_role + - user.role.contributor module: - user -id: user_add_role_action.contributor_role +id: user_add_role_action.contributor label: 'Add the Contributor role role to the selected user(s)' type: user plugin: user_add_role_action configuration: - rid: contributor_role + rid: contributor diff --git a/config/system.action.user_remove_role_action.contributor_role.yml b/config/system.action.user_remove_role_action.contributor.yml similarity index 70% rename from config/system.action.user_remove_role_action.contributor_role.yml rename to config/system.action.user_remove_role_action.contributor.yml index ea5689c22..efe9281cb 100644 --- a/config/system.action.user_remove_role_action.contributor_role.yml +++ b/config/system.action.user_remove_role_action.contributor.yml @@ -3,12 +3,12 @@ langcode: en status: true dependencies: config: - - user.role.contributor_role + - user.role.contributor module: - user -id: user_remove_role_action.contributor_role +id: user_remove_role_action.contributor label: 'Remove the Contributor role role from the selected user(s)' type: user plugin: user_remove_role_action configuration: - rid: contributor_role + rid: contributor diff --git a/config/user.role.contributor_role.yml b/config/user.role.contributor.yml similarity index 81% rename from config/user.role.contributor_role.yml rename to config/user.role.contributor.yml index a9c73ae4d..3b1b50e29 100644 --- a/config/user.role.contributor_role.yml +++ b/config/user.role.contributor.yml @@ -6,8 +6,8 @@ dependencies: - node.type.report module: - node -id: contributor_role -label: 'Contributor role' +id: contributor +label: 'Contributor' weight: 9 is_admin: null permissions: diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 5cb69c48c..e01fa6008 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -79,8 +79,8 @@ public function testAddReportAsContributorDraftAllowed() { $title = $this->randomMachineName(32); $user = $this->createUserIfNeeded(2884910, 'report allowed'); - if (!$user->hasRole('contributor_role')) { - $user->addRole('contributor_role'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); $user->save(); } $this->drupalLogin($user); @@ -134,8 +134,8 @@ public function testAddReportAsContributorSubmitAllowed() { $title = $this->randomMachineName(32); $user = $this->createUserIfNeeded(2884910, 'report allowed'); - if (!$user->hasRole('contributor_role')) { - $user->addRole('contributor_role'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); $user->save(); } $this->drupalLogin($user); @@ -189,8 +189,8 @@ public function testAddReportAsContributorDraftTrusted() { $title = $this->randomMachineName(32); $user = $this->createUserIfNeeded(2884910, 'report trusted'); - if (!$user->hasRole('contributor_role')) { - $user->addRole('contributor_role'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); $user->save(); } $this->drupalLogin($user); @@ -244,8 +244,8 @@ public function testAddReportAsContributorSubmitTrusted() { $title = $this->randomMachineName(32); $user = $this->createUserIfNeeded(2884910, 'report trusted'); - if (!$user->hasRole('contributor_role')) { - $user->addRole('contributor_role'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); $user->save(); } $this->drupalLogin($user); diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index a8358274e..b842c2df7 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -290,7 +290,7 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en } } - if (UserHelper::userHasRoles(['contributor_role'])) { + if (UserHelper::userHasRoles(['contributor'])) { // Warning message when saving as a draft. if (isset($buttons['draft'])) { $message = $this->t('You are saving this document as a draft. It will not be visible to visitors. If you wish to proceed with the publication kindly click on @buttons instead.', [ diff --git a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php index 6cc633cd8..ec079f5b9 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php +++ b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php @@ -30,7 +30,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ?Moderati // Link to create a new entity. $url_options = ['attributes' => ['target' => '_blank']]; - if ($user && $user->hasRole('contributor_role')) { + if ($user && $user->hasRole('contributor')) { $links = $this->t('Create a new Job vacancy, a new Training program or a new Report', [ '@job_url' => Url::fromRoute('node.add', [ 'node_type' => 'job', From d3f2e7905adc4dade3f55cbd5088b405e7b1b840 Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Tue, 1 Oct 2024 10:53:17 +0200 Subject: [PATCH 27/79] Update html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php Co-authored-by: orakili --- .../reliefweb_moderation/src/Services/ReportModeration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index b842c2df7..093208a84 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -285,7 +285,7 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en // Add confirmation when attempting to change published document. if ($status === 'published') { - $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The report may be set as to-review.'); + $message = $this->t('Press OK to submit the changes for review by the ReliefWeb editors. The report may become unpublished while being reviewed.'); $buttons['to-review']['#attributes']['onclick'] = 'return confirm("' . $message . '")'; } } From b066d00e11e2ce42149dd68ddfb0f679431ffb6b Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 10:56:48 +0200 Subject: [PATCH 28/79] chore: Limit filter options depending on permission --- .../src/Services/UserPostsService.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index 1300e99e2..e774451e7 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -262,6 +262,14 @@ protected function initFilterDefinitions(array $filters = []) { ]; // Filter by bundle. + $allowed_bundles = [ + 'job' => $this->t('Job'), + 'training' => $this->t('Training'), + ]; + if ($this->currentUser->hasPermission('create report content')) { + $allowed_bundles['report'] = $this->t('Report'); + } + $definitions['bundle'] = [ 'type' => 'property', 'field' => 'type', @@ -269,11 +277,7 @@ protected function initFilterDefinitions(array $filters = []) { 'shortcut' => 'ty', 'form' => 'other', 'operator' => 'OR', - 'values' => [ - 'job' => $this->t('Job'), - 'training' => $this->t('Training'), - 'report' => $this->t('Report'), - ], + 'values' => $allowed_bundles, ]; // Limit sources. From 07b1badd4d0b2f071f3d9aa2b7d75c4ea71e8628 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 11:55:01 +0200 Subject: [PATCH 29/79] chore: Add refused, add own version of updateModerationStatusFromPostingRights --- .../reliefweb_entities/src/Entity/Report.php | 82 +++++++ .../src/OpportunityDocumentTrait.php | 2 +- .../src/ExistingSite/RwReportAddTest.php | 224 +++++++++++++++++- .../src/Services/ReportModeration.php | 3 +- 4 files changed, 307 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 7758e15e2..1a8463603 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -14,11 +14,14 @@ use Drupal\reliefweb_entities\OpportunityDocumentTrait; use Drupal\reliefweb_moderation\EntityModeratedInterface; use Drupal\reliefweb_moderation\EntityModeratedTrait; +use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; use Drupal\reliefweb_revisions\EntityRevisionedInterface; use Drupal\reliefweb_revisions\EntityRevisionedTrait; use Drupal\reliefweb_utility\Helpers\DateHelper; use Drupal\reliefweb_utility\Helpers\ReliefWebStateHelper; +use Drupal\reliefweb_utility\Helpers\TaxonomyHelper; use Drupal\reliefweb_utility\Helpers\UrlHelper; +use Drupal\reliefweb_utility\Helpers\UserHelper; /** * Bundle class for report nodes. @@ -356,4 +359,83 @@ protected function sendPublicationNotification() { ->mail('reliefweb_entities', 'report_publication_notification', $to, $langcode, $parameters, $from, TRUE); } + /** + * Update the status for the entity based on the user posting rights. + */ + protected function updateModerationStatusFromPostingRights() { + // In theory the revision user here, is the current user saving the entity. + /** @var \Drupal\user\UserInterface|null $user */ + $user = $this->getRevisionUser(); + $status = $this->getModerationStatus(); + + // Skip if there is no revision user. That should normally not happen with + // new content but some old revisions may reference users that don't exist + // anymore (which should not happen either but...). + if (empty($user)) { + return; + } + + // For non editors, we determine the real status based on the user + // posting rights for the selected sources. + if (!UserHelper::userHasRoles(['editor'], $user)) { + // Retrieve the list of sources and check the user rights. + if (!$this->field_source->isEmpty()) { + // Extract source ids. + $sources = []; + foreach ($this->field_source as $item) { + if (!empty($item->target_id)) { + $sources[] = $item->target_id; + } + } + + // Get the user's posting right for the document. + $right = UserPostingRightsHelper::getUserConsolidatedPostingRight($user, $this->bundle(), $sources); + + // Update the status based on the user's right. + // Note: we don't use `t()` because those are log messages for editors. + switch ($right['name']) { + // Unverified for some sources => on-hold + flag. + case 'unverified': + $status = 'on-hold'; + $message = strtr('Unverified user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($right['sources'])), + ]); + break; + + // Blocked for some sources => refused + flag. + case 'blocked': + $status = 'refused'; + $message = strtr('Blocked user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($right['sources'])), + ]); + break; + + // Allowed for all sources => on-hold. + case 'allowed': + $status = 'to-review'; + break; + + // Trusted for all the sources => published. + case 'trusted': + $status = 'published'; + break; + } + + $this->setModerationStatus($status); + + // Update the log message. + if (!empty($message)) { + $revision_log_field = $this->getEntityType() + ->getRevisionMetadataKey('revision_log_message'); + + if (!empty($revision_log_field)) { + $log = trim($this->{$revision_log_field}->value ?? ''); + $log = $message . (!empty($log) ? ' ' . $log : ''); + $this->{$revision_log_field}->value = $log; + } + } + } + } + } + } diff --git a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php index b303bb768..4ac2a5273 100644 --- a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php @@ -9,7 +9,7 @@ use Drupal\reliefweb_utility\Helpers\UserHelper; /** - * Trait for "opportunity" documents like jobs, trainings and reports. + * Trait for "opportunity" documents like jobs and trainings. * * @see Drupal\reliefweb_entities\DocuemntInterface */ diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index e01fa6008..a6ea6f362 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -71,6 +71,226 @@ public function testAddReportAsAdminDraft() { $this->assertSession()->statusCodeEquals(404); } + /** + * Test adding a report as contributor, draft, unverified. + */ + public function testAddReportAsContributorDraftUnverified() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report unverified'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '0', // Unverified. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Save as draft'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'on-hold'); + } + + /** + * Test adding a report as contributor, submit, unverified. + */ + public function testAddReportAsContributorSubmitUnverified() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report unverified'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '0', // Unverified. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); + } + + /** + * Test adding a report as contributor, draft, blocked. + */ + public function testAddReportAsContributorDraftBlocked() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report blocked'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '1', // Blocked. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Save as draft'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'refused'); + } + + /** + * Test adding a report as contributor, submit, blocked. + */ + public function testAddReportAsContributorSubmitBlocked() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $user = $this->createUserIfNeeded(2884910, 'report blocked'); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); + $user->save(); + } + $this->drupalLogin($user); + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => '1', // Blocked. + 'notes' => '', + ], + ]); + $term_source->save(); + + $edit = $this->getEditFields($title); + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('ABC Color'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); + } + /** * Test adding a report as contributor, draft, allowed. */ @@ -123,7 +343,7 @@ public function testAddReportAsContributorDraftAllowed() { $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'draft'); + $this->assertEquals($node->moderation_status->value, 'on-hold'); } /** @@ -288,7 +508,7 @@ public function testAddReportAsContributorSubmitTrusted() { $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'to-review'); + $this->assertEquals($node->moderation_status->value, 'published'); } protected function getEditFields($title) { diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 093208a84..682b21939 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -206,6 +206,7 @@ public function getStatuses() { 'to-review' => $this->t('To review'), 'published' => $this->t('Published'), 'embargoed' => $this->t('Embargoed'), + 'refused' => $this->t('Refused'), 'archive' => $this->t('Archived'), 'reference' => $this->t('Reference'), ]; @@ -316,7 +317,7 @@ public function isPublishedStatus($status) { * {@inheritdoc} */ public function isEditableStatus($status, ?AccountInterface $account = NULL) { - if ($status === 'archive') { + if ($status === 'archive' || $status === 'refused') { return UserHelper::userHasRoles(['administrator', 'webmaster'], $account); } return TRUE; From ad431bf9939ac47ecef21825e2490a5674745d36 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 12:15:07 +0200 Subject: [PATCH 30/79] chore: Add refused, add own version of updateModerationStatusFromPostingRights --- .../reliefweb_entities/src/Entity/Report.php | 2 +- .../tests/src/ExistingSite/RwReportAddTest.php | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 1a8463603..3a9e1d6b4 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -377,7 +377,7 @@ protected function updateModerationStatusFromPostingRights() { // For non editors, we determine the real status based on the user // posting rights for the selected sources. - if (!UserHelper::userHasRoles(['editor'], $user)) { + if (!UserHelper::userHasRoles(['editor'], $user) && in_array($status, ['on-hold', 'to-review'])) { // Retrieve the list of sources and check the user rights. if (!$this->field_source->isEmpty()) { // Extract source ids. diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index a6ea6f362..6ef2434d4 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -123,7 +123,7 @@ public function testAddReportAsContributorDraftUnverified() { $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'on-hold'); + $this->assertEquals($node->moderation_status->value, 'draft'); } /** @@ -175,10 +175,10 @@ public function testAddReportAsContributorSubmitUnverified() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'to-review'); + $this->assertEquals($node->moderation_status->value, 'on-hold'); } /** @@ -233,7 +233,7 @@ public function testAddReportAsContributorDraftBlocked() { $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'refused'); + $this->assertEquals($node->moderation_status->value, 'draft'); } /** @@ -285,10 +285,10 @@ public function testAddReportAsContributorSubmitBlocked() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'to-review'); + $this->assertEquals($node->moderation_status->value, 'refused'); } /** @@ -343,7 +343,7 @@ public function testAddReportAsContributorDraftAllowed() { $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'on-hold'); + $this->assertEquals($node->moderation_status->value, 'draft'); } /** From 43f1bba04f14aefe204efedaa5092f84d6ce8ae8 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 13:49:03 +0200 Subject: [PATCH 31/79] chore: Add refused, add own version of updateModerationStatusFromPostingRights --- .../reliefweb_entities.module | 4 +- .../src/ExistingSite/RwReportCreateTest.php | 272 +++++++++++++++++- 2 files changed, 265 insertions(+), 11 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/reliefweb_entities.module b/html/modules/custom/reliefweb_entities/reliefweb_entities.module index c594ede42..ad304ba55 100644 --- a/html/modules/custom/reliefweb_entities/reliefweb_entities.module +++ b/html/modules/custom/reliefweb_entities/reliefweb_entities.module @@ -96,7 +96,9 @@ function reliefweb_entities_entity_bundle_info_alter(&$bundles) { } if ($class !== FALSE && is_subclass_of($class, BundleEntityInterface::class)) { $label = ucwords(str_replace(['_', '-'], ' ', $bundle)); - $bundles[$entity_type_id][$bundle]['class'] = $class; + // No leading \ otherwise EntityTypeRepository::getEntityTypeFromClass + // fails. + $bundles[$entity_type_id][$bundle]['class'] = substr($class, 1); $bundles[$entity_type_id][$bundle]['label'] = $label; } } diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index 0cf3c3940..29a7440c1 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -4,43 +4,295 @@ namespace Drupal\Tests\reliefweb_entities\ExistingSite; -use Drupal\node\Entity\Node; +use Drupal\reliefweb_entities\Entity\Report; +use Drupal\taxonomy\Entity\Term; use Drupal\user\Entity\User; -use weitzman\DrupalTestTraits\ExistingSiteBase; /** * Tests reports. */ -class RwReportCreateTest extends ExistingSiteBase { +class RwReportCreateTest extends RwReportBase { /** * Test report. */ - public function testReport() { + public function testCreateReportAsAdminDraft() { $site_name = \Drupal::config('system.site')->get('name'); $title = 'My report'; + $user = User::load(1); + $this->drupalLogin($user); - $report = Node::create([ + $term_language = $this->createTermIfNeeded('language', 267, 'English'); + $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); + $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + $report = Report::create([ + 'uid' => $user->id(), 'type' => 'report', 'title' => $title, + 'moderation_status' => 'draft', 'field_origin' => 0, 'field_origin_notes' => 'https://www.example.com/my-report', + 'field_language' => $term_language->id(), + 'field_country' => [ + $term_country->id(), + ], + 'field_primary_country' => $term_country->id(), + 'field_content_format' => $term_format->id(), + 'field_source' => [ + $term_source->id(), + ], ]); // Report will be saved as draft. - $report->setPublished()->save(); + $report->save(); + + // OK for user. + $this->drupalGet($report->toUrl()); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->elementTextEquals('css', '.rw-article__title.rw-page-title', $title); // 404 for anonymous. + $this->drupalGet('user/logout'); $this->drupalGet($report->toUrl()); $this->assertSession()->statusCodeEquals(404); + } + + /** + * Test report. + */ + public function testCreateReportAsAdminPublished() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = 'My report'; + $user = User::load(1); + $this->drupalLogin($user); + + $term_language = $this->createTermIfNeeded('language', 267, 'English'); + $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); + $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + $report = Report::create([ + 'uid' => $user->id(), + 'type' => 'report', + 'title' => $title, + 'moderation_status' => 'published', + 'field_origin' => 0, + 'field_origin_notes' => 'https://www.example.com/my-report', + 'field_language' => $term_language->id(), + 'field_country' => [ + $term_country->id(), + ], + 'field_primary_country' => $term_country->id(), + 'field_content_format' => $term_format->id(), + 'field_source' => [ + $term_source->id(), + ], + ]); + + // Report will be saved as published. + $report->save(); + + // OK for user. + $this->drupalGet($report->toUrl()); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->elementTextEquals('css', '.rw-article__title.rw-page-title', $title); + + // 404 for anonymous. + $this->drupalGet('user/logout'); + $this->drupalGet($report->toUrl()); + $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->elementTextEquals('css', '.rw-article__title.rw-page-title', $title); + } + + /** + * Test report as contributor unverified, draft. + */ + public function testCreateReportAsContributorUnverifiedDraft() { + $title = 'My report - unverified'; + $this->setUserPostingRights(0, 'Unverified'); + $moderation_status = 'draft'; + $expected_moderation_status = 'draft'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor unverified, to-review. + */ + public function testCreateReportAsContributorUnverifiedToReview() { + $title = 'My report - unverified'; + $this->setUserPostingRights(0, 'Unverified'); + $moderation_status = 'to-review'; + $expected_moderation_status = 'on-hold'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor blocked, draft. + */ + public function testCreateReportAsContributorBlockedDraft() { + $title = 'My report - blocked'; + $this->setUserPostingRights(1, 'blocked'); + $moderation_status = 'draft'; + $expected_moderation_status = 'draft'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor blocked, to-review. + */ + public function testCreateReportAsContributorBlockedToReview() { + $title = 'My report - blocked'; + $this->setUserPostingRights(1, 'blocked'); + $moderation_status = 'to-review'; + $expected_moderation_status = 'refused'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor allowed, draft. + */ + public function testCreateReportAsContributorAllowedDraft() { + $title = 'My report - allowed'; + $this->setUserPostingRights(2, 'allowed'); + $moderation_status = 'draft'; + $expected_moderation_status = 'draft'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor allowed, to-review. + */ + public function testCreateReportAsContributorAllowedToReview() { + $title = 'My report - allowed'; + $this->setUserPostingRights(2, 'allowed'); + $moderation_status = 'to-review'; + $expected_moderation_status = 'to-review'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, TRUE); + } + + /** + * Test report as contributor trusted, draft. + */ + public function testCreateReportAsContributorTrustedDraft() { + $title = 'My report - trusted'; + $this->setUserPostingRights(3, 'trusted'); + $moderation_status = 'draft'; + $expected_moderation_status = 'draft'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + } + + /** + * Test report as contributor trusted, to-review. + */ + public function testCreateReportAsContributorTrustedToReview() { + $title = 'My report - trusted'; + $this->setUserPostingRights(3, 'trusted'); + $moderation_status = 'to-review'; + $expected_moderation_status = 'published'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, TRUE); + } - // OK for admins. - $admin = User::load(1); - $this->drupalLogin($admin); + /** + * Test report as contributor. + */ + protected function runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, $will_be_public) { + $site_name = \Drupal::config('system.site')->get('name'); + + $user = User::load(2884910); + $this->drupalLogin($user); + + $term_language = $this->createTermIfNeeded('language', 267, 'English'); + $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); + $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); + $term_source = Term::load(43679); + + $report = Report::create([ + 'uid' => $user->id(), + 'revision_uid' => $user->id(), + 'type' => 'report', + 'title' => $title, + 'moderation_status' => $moderation_status, + 'field_origin' => 0, + 'field_origin_notes' => 'https://www.example.com/my-report', + 'field_language' => $term_language->id(), + 'field_country' => [ + $term_country->id(), + ], + 'field_primary_country' => $term_country->id(), + 'field_content_format' => $term_format->id(), + 'field_source' => [ + $term_source->id(), + ], + ]); + // Report will be saved as draft. + $report->save(); + + // OK for user. $this->drupalGet($report->toUrl()); - $this->assertSession()->titleEquals($title . ' | ' . $site_name); + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); $this->assertSession()->elementTextEquals('css', '.rw-article__title.rw-page-title', $title); + + // Test for anonymous. + $this->drupalGet('user/logout'); + $this->drupalGet($report->toUrl()); + if ($will_be_public) { + $this->assertSession()->statusCodeEquals(200); + } + else { + $this->assertSession()->statusCodeEquals(404); + } + + // Check moderation status. + $this->assertEquals($report->moderation_status->value, $expected_moderation_status); } + /** + * Set user posting rights. + */ + protected function setUserPostingRights($right, $label) { + $user = $this->createUserIfNeeded(2884910, $label); + if (!$user->hasRole('contributor')) { + $user->addRole('contributor'); + $user->save(); + } + + // Create term first so we can assign posting rights. + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $term_source->set('field_user_posting_rights', [ + [ + 'id' => $user->id(), + 'job' => '0', + 'training' => '0', + 'report' => $right, + 'notes' => '', + ], + ]); + $term_source->save(); + } } From 8479003b4cccb4d663c18656d0071d0d9651e34f Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 14:05:03 +0200 Subject: [PATCH 32/79] chore: Fix static --- .../tests/src/ExistingSite/RwReportCreateTest.php | 2 ++ .../src/Helpers/UserPostingRightsHelper.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index 29a7440c1..c12efb39d 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -294,5 +294,7 @@ protected function setUserPostingRights($right, $label) { ], ]); $term_source->save(); + + drupal_static_reset('reliefweb_moderation_getUserPostingRights'); } } diff --git a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php index 65fb7425b..99f17ff9f 100644 --- a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php +++ b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php @@ -98,7 +98,7 @@ public static function getEntityAuthorPostingRights(EntityInterface $entity) { * Posting rights as an associative array keyed by source id. */ public static function getUserPostingRights(?AccountInterface $account = NULL, array $sources = []) { - static $users; + $users = &drupal_static('reliefweb_moderation_getUserPostingRights'); $helper = new UserPostingRightsHelper(); $account = $account ?: \Drupal::currentUser(); From fab897d21460345fd31aa1bb0bdf345b15bc29f1 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 15:06:16 +0200 Subject: [PATCH 33/79] feat: Allow contributor to upload media --- config/user.role.contributor.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config/user.role.contributor.yml b/config/user.role.contributor.yml index 3b1b50e29..96318fda0 100644 --- a/config/user.role.contributor.yml +++ b/config/user.role.contributor.yml @@ -3,12 +3,17 @@ langcode: en status: true dependencies: config: + - media.type.image_report - node.type.report module: + - media - node id: contributor -label: 'Contributor' +label: Contributor weight: 9 is_admin: null permissions: + - 'create image_report media' - 'create report content' + - 'delete own image_report media' + - 'edit own image_report media' From d03c8b3bf80274df2d0f79808e36846373f4c441 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 15:10:45 +0200 Subject: [PATCH 34/79] feat: Allow contributor to edit own --- config/user.role.contributor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/user.role.contributor.yml b/config/user.role.contributor.yml index 96318fda0..481980e94 100644 --- a/config/user.role.contributor.yml +++ b/config/user.role.contributor.yml @@ -17,3 +17,4 @@ permissions: - 'create report content' - 'delete own image_report media' - 'edit own image_report media' + - 'edit own report content' From a6b8a713634124bd80716428c6b4d1d975daa7d0 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 15:46:39 +0200 Subject: [PATCH 35/79] bug: namespace --- .../custom/reliefweb_entities/reliefweb_entities.module | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/reliefweb_entities.module b/html/modules/custom/reliefweb_entities/reliefweb_entities.module index ad304ba55..637214902 100644 --- a/html/modules/custom/reliefweb_entities/reliefweb_entities.module +++ b/html/modules/custom/reliefweb_entities/reliefweb_entities.module @@ -86,7 +86,7 @@ function reliefweb_entities_entity_bundle_field_info_alter(array &$fields, Entit * Add bundle classes to nodes and terms to handle business logic. */ function reliefweb_entities_entity_bundle_info_alter(&$bundles) { - $namespace = '\Drupal\reliefweb_entities\Entity'; + $namespace = 'Drupal\reliefweb_entities\Entity'; foreach ($bundles as $entity_type_id => $items) { foreach ($items as $bundle => $info) { @@ -98,7 +98,7 @@ function reliefweb_entities_entity_bundle_info_alter(&$bundles) { $label = ucwords(str_replace(['_', '-'], ' ', $bundle)); // No leading \ otherwise EntityTypeRepository::getEntityTypeFromClass // fails. - $bundles[$entity_type_id][$bundle]['class'] = substr($class, 1); + $bundles[$entity_type_id][$bundle]['class'] = $class; $bundles[$entity_type_id][$bundle]['label'] = $label; } } From ebc48f6bd4b02d177f0cca4ac5979d0ff9f67c68 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 1 Oct 2024 15:51:00 +0200 Subject: [PATCH 36/79] feat: Allow contributor to edit own --- config/user.role.contributor.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/user.role.contributor.yml b/config/user.role.contributor.yml index 481980e94..8df408dcc 100644 --- a/config/user.role.contributor.yml +++ b/config/user.role.contributor.yml @@ -15,6 +15,9 @@ is_admin: null permissions: - 'create image_report media' - 'create report content' + - 'delete media' - 'delete own image_report media' - 'edit own image_report media' - 'edit own report content' + - 'update media' + - 'view own unpublished media' From 893ca82afe0cfa40c2a9aa2478a668b338370adc Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 2 Oct 2024 11:12:03 +0200 Subject: [PATCH 37/79] chore: Move updateModerationStatusFromSourceStatus to DocumentTrait.php --- .../reliefweb_entities/src/DocumentTrait.php | 39 ++++++++++++++++++ .../src/OpportunityDocumentTrait.php | 40 ------------------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/DocumentTrait.php b/html/modules/custom/reliefweb_entities/src/DocumentTrait.php index 8d2098a4d..dee23b8db 100644 --- a/html/modules/custom/reliefweb_entities/src/DocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/DocumentTrait.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityPublishedInterface; +use Drupal\Core\Entity\RevisionLogInterface; use Drupal\Core\Field\EntityReferenceFieldItemList; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Url; @@ -394,4 +395,42 @@ protected function updateSourceModerationStatus() { } } + /** + * Update the status to refused if any of the sources is blocked. + */ + protected function updateModerationStatusFromSourceStatus() { + if (!$this->hasField('field_source') || $this->field_source->isEmpty()) { + return; + } + + $blocked = []; + foreach ($this->field_source as $item) { + $source = $item->entity; + if (empty($source) || !($source instanceof Source)) { + continue; + } + + if ($source->getModerationStatus() === 'blocked') { + $blocked[] = $source->label(); + } + } + + if (!empty($blocked)) { + $this->setModerationStatus('refused'); + + // Add a message to the revision log. + if ($this instanceof RevisionLogInterface) { + $message = 'Submissions from "' . implode('", "', $blocked) . '" are no longer allowed.'; + + $log = $this->getRevisionLogMessage(); + if (empty($log)) { + $this->setRevisionLogMessage($message); + } + else { + $this->setRevisionLogMessage($message . ' ' . $log); + } + } + } + } + } diff --git a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php index 4ac2a5273..03c4126df 100644 --- a/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php +++ b/html/modules/custom/reliefweb_entities/src/OpportunityDocumentTrait.php @@ -2,8 +2,6 @@ namespace Drupal\reliefweb_entities; -use Drupal\Core\Entity\RevisionLogInterface; -use Drupal\reliefweb_entities\Entity\Source; use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; use Drupal\reliefweb_utility\Helpers\TaxonomyHelper; use Drupal\reliefweb_utility\Helpers\UserHelper; @@ -103,44 +101,6 @@ protected function updateModerationStatusFromExpirationDate() { } } - /** - * Update the status to refused if any of the sources is blocked. - */ - protected function updateModerationStatusFromSourceStatus() { - if (!$this->hasField('field_source') || $this->field_source->isEmpty()) { - return; - } - - $blocked = []; - foreach ($this->field_source as $item) { - $source = $item->entity; - if (empty($source) || !($source instanceof Source)) { - continue; - } - - if ($source->getModerationStatus() === 'blocked') { - $blocked[] = $source->label(); - } - } - - if (!empty($blocked)) { - $this->setModerationStatus('refused'); - - // Add a message to the revision log. - if ($this instanceof RevisionLogInterface) { - $message = 'Submissions from "' . implode('", "', $blocked) . '" are no longer allowed.'; - - $log = $this->getRevisionLogMessage(); - if (empty($log)) { - $this->setRevisionLogMessage($message); - } - else { - $this->setRevisionLogMessage($message . ' ' . $log); - } - } - } - } - /** * Update creation date when the opportunity is published for the first time. */ From 28406d59296359668f0a6eab3bcfbc99820e1ace Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 2 Oct 2024 11:12:23 +0200 Subject: [PATCH 38/79] chore: Move updateModerationStatusFromSourceStatus to DocumentTrait.php --- html/modules/custom/reliefweb_entities/src/Entity/Report.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 6a6d14d29..9cc4407d8 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -11,7 +11,6 @@ use Drupal\reliefweb_entities\BundleEntityInterface; use Drupal\reliefweb_entities\DocumentInterface; use Drupal\reliefweb_entities\DocumentTrait; -use Drupal\reliefweb_entities\OpportunityDocumentTrait; use Drupal\reliefweb_moderation\EntityModeratedInterface; use Drupal\reliefweb_moderation\EntityModeratedTrait; use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; @@ -31,7 +30,6 @@ class Report extends Node implements BundleEntityInterface, EntityModeratedInter use DocumentTrait; use EntityModeratedTrait; use EntityRevisionedTrait; - use OpportunityDocumentTrait; use StringTranslationTrait; /** From f3bfdf9897ae5c6891eb1009fe9f3f1a18f8f9a7 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 2 Oct 2024 11:15:24 +0200 Subject: [PATCH 39/79] test: Report for blocked source --- .../src/ExistingSite/RwReportCreateTest.php | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index c12efb39d..4cb808165 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -120,7 +120,7 @@ public function testCreateReportAsAdminPublished() { */ public function testCreateReportAsContributorUnverifiedDraft() { $title = 'My report - unverified'; - $this->setUserPostingRights(0, 'Unverified'); + $this->setUserPostingRightsGetSourceTerm(0, 'Unverified'); $moderation_status = 'draft'; $expected_moderation_status = 'draft'; @@ -132,7 +132,7 @@ public function testCreateReportAsContributorUnverifiedDraft() { */ public function testCreateReportAsContributorUnverifiedToReview() { $title = 'My report - unverified'; - $this->setUserPostingRights(0, 'Unverified'); + $this->setUserPostingRightsGetSourceTerm(0, 'Unverified'); $moderation_status = 'to-review'; $expected_moderation_status = 'on-hold'; @@ -144,7 +144,7 @@ public function testCreateReportAsContributorUnverifiedToReview() { */ public function testCreateReportAsContributorBlockedDraft() { $title = 'My report - blocked'; - $this->setUserPostingRights(1, 'blocked'); + $this->setUserPostingRightsGetSourceTerm(1, 'blocked'); $moderation_status = 'draft'; $expected_moderation_status = 'draft'; @@ -156,7 +156,7 @@ public function testCreateReportAsContributorBlockedDraft() { */ public function testCreateReportAsContributorBlockedToReview() { $title = 'My report - blocked'; - $this->setUserPostingRights(1, 'blocked'); + $this->setUserPostingRightsGetSourceTerm(1, 'blocked'); $moderation_status = 'to-review'; $expected_moderation_status = 'refused'; @@ -168,7 +168,7 @@ public function testCreateReportAsContributorBlockedToReview() { */ public function testCreateReportAsContributorAllowedDraft() { $title = 'My report - allowed'; - $this->setUserPostingRights(2, 'allowed'); + $this->setUserPostingRightsGetSourceTerm(2, 'allowed'); $moderation_status = 'draft'; $expected_moderation_status = 'draft'; @@ -180,7 +180,7 @@ public function testCreateReportAsContributorAllowedDraft() { */ public function testCreateReportAsContributorAllowedToReview() { $title = 'My report - allowed'; - $this->setUserPostingRights(2, 'allowed'); + $this->setUserPostingRightsGetSourceTerm(2, 'allowed'); $moderation_status = 'to-review'; $expected_moderation_status = 'to-review'; @@ -192,7 +192,7 @@ public function testCreateReportAsContributorAllowedToReview() { */ public function testCreateReportAsContributorTrustedDraft() { $title = 'My report - trusted'; - $this->setUserPostingRights(3, 'trusted'); + $this->setUserPostingRightsGetSourceTerm(3, 'trusted'); $moderation_status = 'draft'; $expected_moderation_status = 'draft'; @@ -204,17 +204,37 @@ public function testCreateReportAsContributorTrustedDraft() { */ public function testCreateReportAsContributorTrustedToReview() { $title = 'My report - trusted'; - $this->setUserPostingRights(3, 'trusted'); + $this->setUserPostingRightsGetSourceTerm(3, 'trusted'); $moderation_status = 'to-review'; $expected_moderation_status = 'published'; $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, TRUE); } + /** + * Test report as contributor trusted, to-review but blocked source. + */ + public function testCreateReportAsContributorTrustedToReviewBlockedSource() { + $title = 'My report - trusted'; + $term_source = $this->setUserPostingRightsGetSourceTerm(3, 'trusted', 2884910, 999999, 'Blocked source'); + $term_source + ->set('moderation_status', 'blocked') + ->save(); + + $moderation_status = 'to-review'; + $expected_moderation_status = 'refused'; + + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE, [ + 'field_source' => [ + $term_source->id(), + ], + ]); + } + /** * Test report as contributor. */ - protected function runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, $will_be_public) { + protected function runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, $will_be_public, array $overrides = []) { $site_name = \Drupal::config('system.site')->get('name'); $user = User::load(2884910); @@ -225,7 +245,7 @@ protected function runTestCreateReportAsContributor($title, $moderation_status, $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); $term_source = Term::load(43679); - $report = Report::create([ + $report = Report::create($overrides + [ 'uid' => $user->id(), 'revision_uid' => $user->id(), 'type' => 'report', @@ -269,15 +289,15 @@ protected function runTestCreateReportAsContributor($title, $moderation_status, /** * Set user posting rights. */ - protected function setUserPostingRights($right, $label) { - $user = $this->createUserIfNeeded(2884910, $label); + protected function setUserPostingRightsGetSourceTerm($right, $label, $uid = 2884910, $tid = 43679, $term_label = 'ABC Color') : Term { + $user = $this->createUserIfNeeded($uid, $label); if (!$user->hasRole('contributor')) { $user->addRole('contributor'); $user->save(); } // Create term first so we can assign posting rights. - $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + $term_source = $this->createTermIfNeeded('source', $tid, $term_label, [ 'field_allowed_content_types' => [ 1, ], @@ -296,5 +316,7 @@ protected function setUserPostingRights($right, $label) { $term_source->save(); drupal_static_reset('reliefweb_moderation_getUserPostingRights'); + + return $term_source; } } From db6a089268d6b6a51f66b90b4c1817f858759bd2 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 2 Oct 2024 11:52:46 +0200 Subject: [PATCH 40/79] test: Fix path in entraid tests --- .../tests/src/ExistingSite/ReliefwebEntraidLoginTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/html/modules/custom/reliefweb_entraid/tests/src/ExistingSite/ReliefwebEntraidLoginTest.php b/html/modules/custom/reliefweb_entraid/tests/src/ExistingSite/ReliefwebEntraidLoginTest.php index 4ba5e1e9c..509e8bfb1 100644 --- a/html/modules/custom/reliefweb_entraid/tests/src/ExistingSite/ReliefwebEntraidLoginTest.php +++ b/html/modules/custom/reliefweb_entraid/tests/src/ExistingSite/ReliefwebEntraidLoginTest.php @@ -49,6 +49,8 @@ protected function tearDown(): void { * @covers ::redirectLogin() */ public function testRedirectLogin() { + global $base_url; + // Get the EntraID configuration. $entraid_config = $this->container ->get('config.factory') @@ -69,9 +71,9 @@ public function testRedirectLogin() { // Set the endpoints. We just point at the robots.txt as we know it exists // and so, if the reponse status code in 200, then the redirection worked. $data = $entraid_config->get(); - $data['settings']['authorization_endpoint_wa'] = 'http://localhost/robots.txt'; - $data['settings']['token_endpoint_wa'] = 'http://localhost/robots.txt'; - $data['settings']['iss_allowed_domains'] = 'http://localhost/robots.txt'; + $data['settings']['authorization_endpoint_wa'] = $base_url . '/robots.txt'; + $data['settings']['token_endpoint_wa'] = $base_url . '/robots.txt'; + $data['settings']['iss_allowed_domains'] = $base_url . '/robots.txt'; $entraid_config->setData($data)->save(); // If the redirection works, a 200 will be returned. From d9d854b5f2b9136752b2182cccba6d7ba8ab7c50 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 2 Oct 2024 12:11:56 +0200 Subject: [PATCH 41/79] chore: Specify test URL --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index c54b86212..51a8cab4d 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -137,7 +137,7 @@ jobs: docker compose -f tests/docker-compose.yml exec -T drupal chmod -R 777 /srv/www/html/sites/default/files /srv/www/html/sites/default/private docker compose -f tests/docker-compose.yml exec -T drupal mkdir -p /srv/www/html/build/logs docker compose -f tests/docker-compose.yml exec -T drupal chmod -R 777 /srv/www/html/build/logs - docker compose -f tests/docker-compose.yml exec -T -w /srv/www -e XDEBUG_MODE=coverage -e BROWSERTEST_OUTPUT_DIRECTORY=/srv/www/html/sites/default/files/browser_output -e DTT_BASE_URL=http://127.0.0.1 drupal ./vendor/bin/phpunit --coverage-clover /srv/www/html/build/logs/clover.xml --debug + docker compose -f tests/docker-compose.yml exec -T -w /srv/www -e XDEBUG_MODE=coverage -e BROWSERTEST_OUTPUT_DIRECTORY=/srv/www/html/sites/default/files/browser_output -e SIMPLETEST_BASE_URL=http://127.0.0.1 -e DTT_BASE_URL=http://127.0.0.1 drupal ./vendor/bin/phpunit --coverage-clover /srv/www/html/build/logs/clover.xml --debug docker cp "$(docker compose -f tests/docker-compose.yml ps -q drupal)":/srv/www/html/build/logs/clover.xml . env: fail-fast: true From 1ee0d0eb2f182ec5fbe1eca8c0930f06f67fba54 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 7 Oct 2024 09:44:05 +0200 Subject: [PATCH 42/79] feat: Allow user manager to assign contgributor role --- config/user.role.user_manager.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/user.role.user_manager.yml b/config/user.role.user_manager.yml index 48b66cc2e..3bb514901 100644 --- a/config/user.role.user_manager.yml +++ b/config/user.role.user_manager.yml @@ -17,6 +17,7 @@ permissions: - 'administer subscriptions' - 'administer users' - 'assign beta_tester role' + - 'assign contributor role' - 'assign editor role' - 'assign external_disaster_manager role' - 'assign job_importer role' From ed075026cc82efcd288519a2b94a83e959ac7908 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Mon, 14 Oct 2024 14:19:09 +0200 Subject: [PATCH 43/79] feat: Display right indicators Refs: #RW-1096 --- .../src/Controller/UserController.php | 37 +++++++++++++++- .../components/rw-people/rw-people.css | 43 +++++++++---------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/html/modules/custom/reliefweb_users/src/Controller/UserController.php b/html/modules/custom/reliefweb_users/src/Controller/UserController.php index 9b65705f9..a9215068d 100644 --- a/html/modules/custom/reliefweb_users/src/Controller/UserController.php +++ b/html/modules/custom/reliefweb_users/src/Controller/UserController.php @@ -290,8 +290,17 @@ public function getUserSources(array &$users) { $link = Link::fromTextAndUrl($record->name, URL::fromUserInput('/taxonomy/term/' . $record->tid . '/user-posting-rights', [ 'attributes' => ['target' => '_blank'], ])); - $row = '
  • ' . $link->toString() . '
  • '; - $sources[$record->uid][$record->tid] = $row; + $row = [ + '
  • ', + $link->toString(), + '', + '' . $this->getRightsLabel('job', $job) . '', + '' . $this->getRightsLabel('training', $training) . '', + '' . $this->getRightsLabel('report', $report) . '', + '', + '
  • ', + ]; + $sources[$record->uid][$record->tid] = implode(' ', $row); } // Add the formatted list of sources to the user objects. @@ -300,4 +309,28 @@ public function getUserSources(array &$users) { } } + /** + * Get human readable rights. + */ + protected function getRightsLabel(string $type, string $right) { + $types = [ + 'job' => $this->t('Job'), + 'training' => $this->t('Training'), + 'report' => $this->t('Report'), + ]; + $rights = [ + 0 => $this->t('Unverified'), + 1 => $this->t('Blocked'), + 2 => $this->t('Allowed'), + 3 => $this->t('Trusted'), + ]; + + $label = [ + $types[$type], + $rights[$right], + ]; + + return implode(' - ', $label); + } + } diff --git a/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css b/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css index 34a049149..c583023b6 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css +++ b/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css @@ -8,7 +8,8 @@ } .reliefweb-users-form .form-item-job-rights label:after, -.reliefweb-users-form .form-item-training-rights label:after { +.reliefweb-users-form .form-item-training-rights label:after, +.reliefweb-users-form .form-item-report-rights label:after { display: inline-block; width: 12px; height: 12px; @@ -18,19 +19,23 @@ border-radius: 4px; } .reliefweb-users-form .form-item-job-rights input[value="unverified"] + label:after, -.reliefweb-users-form .form-item-training-rights input[value="unverified"] + label:after { +.reliefweb-users-form .form-item-training-rights input[value="unverified"] + label:after, +.reliefweb-users-form .form-item-report-rights input[value="unverified"] + label:after { background: #f49e2c; } .reliefweb-users-form .form-item-job-rights input[value="blocked"] + label:after, -.reliefweb-users-form .form-item-training-rights input[value="blocked"] + label:after { +.reliefweb-users-form .form-item-training-rights input[value="blocked"] + label:after, +.reliefweb-users-form .form-item-report-rights input[value="blocked"] + label:after { background: #da190b; } .reliefweb-users-form .form-item-job-rights input[value="allowed"] + label:after, -.reliefweb-users-form .form-item-training-rights input[value="allowed"] + label:after { +.reliefweb-users-form .form-item-training-rights input[value="allowed"] + label:after, +.reliefweb-users-form .form-item-report-rights input[value="allowed"] + label:after { background: #076d96; } .reliefweb-users-form .form-item-job-rights input[value="trusted"] + label:after, -.reliefweb-users-form .form-item-training-rights input[value="trusted"] + label:after { +.reliefweb-users-form .form-item-training-rights input[value="trusted"] + label:after, +.reliefweb-users-form .form-item-report-rights input[value="trusted"] + label:after { background: #88bb09; } .reliefweb-users-form .form-actions.form-item { @@ -103,32 +108,26 @@ .users-list li + li { border-top: 1px dotted var(--cd-reliefweb-brand-grey--light); } -.users-list li[data-job]:before { + +.posting-rights--wrapper { + display: flex; + gap: 6px; +} + +.posting-rights--wrapper .posting-rights { display: inline-block; - float: right; width: 12px; height: 12px; - margin: 4px 16px 0 0; - content: ""; border-radius: 4px; background: var(--cd-reliefweb-orange); - box-shadow: 16px 0 var(--cd-reliefweb-orange); + color:rgba(0,0,0,0); } -.users-list li[data-job="1"]:before { +.posting-rights--wrapper .posting-rights[data-posting-right="1"] { background: var(--cd-reliefweb-red); } -.users-list li[data-job="2"]:before { +.posting-rights--wrapper .posting-rights[data-posting-right="2"] { background: var(--cd-reliefweb-blue); } -.users-list li[data-job="3"]:before { +.posting-rights--wrapper .posting-rights[data-posting-right="3"] { background: var(--cd-reliefweb-green); } -.users-list li[data-training="1"]:before { - box-shadow: 16px 0 var(--cd-reliefweb-red); -} -.users-list li[data-training="2"]:before { - box-shadow: 16px 0 var(--cd-reliefweb-blue); -} -.users-list li[data-training="3"]:before { - box-shadow: 16px 0 var(--cd-reliefweb-green); -} From a88a08f40460ac467066ad8141fdc54d856b16f9 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 15 Oct 2024 15:36:06 +0200 Subject: [PATCH 44/79] feat: access private files Refs: #RW-1100 --- config/user.role.contributor.yml | 2 ++ .../src/Controller/ImageStyleDownloadController.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/config/user.role.contributor.yml b/config/user.role.contributor.yml index 8df408dcc..2c7b2f361 100644 --- a/config/user.role.contributor.yml +++ b/config/user.role.contributor.yml @@ -8,11 +8,13 @@ dependencies: module: - media - node + - reliefweb_files id: contributor label: Contributor weight: 9 is_admin: null permissions: + - 'access reliefweb private files' - 'create image_report media' - 'create report content' - 'delete media' diff --git a/html/modules/custom/reliefweb_files/src/Controller/ImageStyleDownloadController.php b/html/modules/custom/reliefweb_files/src/Controller/ImageStyleDownloadController.php index 6af910a20..504b0ced0 100644 --- a/html/modules/custom/reliefweb_files/src/Controller/ImageStyleDownloadController.php +++ b/html/modules/custom/reliefweb_files/src/Controller/ImageStyleDownloadController.php @@ -122,7 +122,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st // Let other modules handle the file if it's not a file matching the pattern // used for the reliefweb files. if (preg_match($pattern, $uri) !== 1) { - return parent::deliver($request, $scheme, $image_style); + return parent::deliver($request, $scheme, $image_style, $required_derivative_scheme); } // Check the image token. We return a 404 as it's more likely to be cached From d597d81517345e2beca0b4d9cb3d4825d46a4982 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 15 Oct 2024 15:40:28 +0200 Subject: [PATCH 45/79] feat: grant access to display source attention messages Refs: #RW-1101 --- config/user.role.contributor.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/user.role.contributor.yml b/config/user.role.contributor.yml index 2c7b2f361..e702fb568 100644 --- a/config/user.role.contributor.yml +++ b/config/user.role.contributor.yml @@ -9,6 +9,7 @@ dependencies: - media - node - reliefweb_files + - reliefweb_form id: contributor label: Contributor weight: 9 @@ -19,6 +20,7 @@ permissions: - 'create report content' - 'delete media' - 'delete own image_report media' + - 'display source attention messages' - 'edit own image_report media' - 'edit own report content' - 'update media' From cbdcdc98788ff59d9b80ce6be48932a63d9c37df Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 15 Oct 2024 15:44:24 +0200 Subject: [PATCH 46/79] bug: trim on null Refs: #RW-1101 --- html/modules/custom/reliefweb_entities/src/Entity/Report.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 9cc4407d8..77927e777 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -250,7 +250,7 @@ public function preSave(EntityStorageInterface $storage) { '@date' => DateHelper::format($this->field_embargo_date->value, 'custom', 'd M Y H:i e'), ]); - $log = trim($this->getRevisionLogMessage()); + $log = !empty($this->getRevisionLogMessage()) ? trim($this->getRevisionLogMessage()) : ''; $log = $message . (!empty($log) ? "\n" . $log : ''); $this->setRevisionLogMessage($log); } From 6ccae20baef2c18f5e69bb8209bccbe6bcf8ce95 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 15 Oct 2024 16:15:16 +0200 Subject: [PATCH 47/79] feat: Add to-review to filters Refs: #RW-1102 --- .../reliefweb_user_posts/src/Services/UserPostsService.php | 1 + .../components/rw-moderation/rw-moderation.css | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index e774451e7..5ce9ba4a6 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -117,6 +117,7 @@ public function getStatuses() { 'refused' => $this->t('refused'), 'expired' => $this->t('expired'), 'duplicate' => $this->t('duplicate'), + 'to-review' => $this->t('to-review'), ]; } diff --git a/html/themes/custom/common_design_subtheme/components/rw-moderation/rw-moderation.css b/html/themes/custom/common_design_subtheme/components/rw-moderation/rw-moderation.css index da4f10add..7bd28d423 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-moderation/rw-moderation.css +++ b/html/themes/custom/common_design_subtheme/components/rw-moderation/rw-moderation.css @@ -27,7 +27,7 @@ } .rw-moderation-filters .rw-moderation-filter-status .form-checkboxes { display: grid; - grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); + grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); grid-gap: 0 24px; margin-bottom: -12px; } From 282432306625ef6f95cd141b6442893a144a3df9 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 15 Oct 2024 16:31:04 +0200 Subject: [PATCH 48/79] feat: Link on dashboard Refs: #RW-1104 --- .../src/Form/UserPostsPageFilterForm.php | 2 +- .../components/rw-user/rw-user.css | 1 + .../templates/user/user.html.twig | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php index ec079f5b9..cece24eab 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php +++ b/html/modules/custom/reliefweb_user_posts/src/Form/UserPostsPageFilterForm.php @@ -30,7 +30,7 @@ public function buildForm(array $form, FormStateInterface $form_state, ?Moderati // Link to create a new entity. $url_options = ['attributes' => ['target' => '_blank']]; - if ($user && $user->hasRole('contributor')) { + if ($user && $user->hasPermission('create report content')) { $links = $this->t('Create a new Job vacancy, a new Training program or a new Report', [ '@job_url' => Url::fromRoute('node.add', [ 'node_type' => 'job', diff --git a/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css b/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css index 573570694..67dbf4496 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css +++ b/html/themes/custom/common_design_subtheme/components/rw-user/rw-user.css @@ -52,6 +52,7 @@ } .rw-user-dashboard li + li { margin-top: 32px; + break-inside: avoid; } [dir="ltr"] .rw-user-dashboard li { padding-right: 32px; diff --git a/html/themes/custom/common_design_subtheme/templates/user/user.html.twig b/html/themes/custom/common_design_subtheme/templates/user/user.html.twig index 8027abb72..a01c893ed 100644 --- a/html/themes/custom/common_design_subtheme/templates/user/user.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/user/user.html.twig @@ -36,14 +36,24 @@ {% trans %}Manage your bookmarks{% endtrans %}

    {% trans %}Track content relevant to you on the site.{% endtrans %}

    + {% if user.hasPermission('create job content') %}
  • {% trans %}Post a job vacancy{% endtrans %}

    {% trans %}Advertise job, consulting and internships vacancies.{% endtrans %}

  • + {% if user.hasPermission('create training content') %} + {% endif %}
  • {% trans %}Post a training program{% endtrans %}

    {% trans %}Advertise training programs for the humanitarian community.{% endtrans %}

  • + {% endif %} + {% if user.hasPermission('create report content') %} +
  • + {% trans %}Post a report{% endtrans %} +

    {% trans %}Post a report for the humanitarian community.{% endtrans %}

    +
  • + {% endif %}
  • {% trans %}View your posts{% endtrans %}

    {% trans %}Manage the Job vacancies or Training programs you posted.{% endtrans %}

    From bfef48b601a81db35f53ca383092baef16175ece Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 16 Oct 2024 10:27:50 +0200 Subject: [PATCH 49/79] feat: Add save as draft for published Refs: #RW-1106 --- .../reliefweb_moderation/src/Services/ReportModeration.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 682b21939..9fbe7d04a 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -280,6 +280,10 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en } // Other users can submit for review (or publish directly if trusted). else { + $buttons['draft'] = [ + '#value' => $this->t('Save as draft'), + ]; + $buttons['to-review'] = [ '#value' => $new ? $this->t('Submit') : $this->t('Submit changes'), ]; From 278076f93a1b852375576e3099be0459d533ef4b Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 16 Oct 2024 11:54:42 +0200 Subject: [PATCH 50/79] test: Add tests for multiple sources Refs: #RW-1099 --- .../RwReportAddMultipleSourcesTest.php | 426 ++++++++++++++++++ 1 file changed, 426 insertions(+) create mode 100644 html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php new file mode 100644 index 000000000..c5c144bb2 --- /dev/null +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php @@ -0,0 +1,426 @@ +contributor = $this->createUserIfNeeded(2884910, 'report unverified'); + if (!$this->contributor->hasRole('contributor')) { + $this->contributor->addRole('contributor'); + $this->contributor->save(); + } + + $rights = [ + 0 => 'unverified', + 1 => 'blocked', + 2 => 'allowed', + 3 => 'trusted', + ]; + + foreach ($rights as $id => $right) { + $label = 'Src ' . $right; + $field_name = 'source_' . $right; + + // Create term and assign rights. + $this->{$field_name} = $this->createTermIfNeeded('source', 9999900 + $id, $label, [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Set posting right to + $this->{$field_name}->set('field_user_posting_rights', [ + [ + 'id' => $this->contributor->id(), + 'job' => '0', + 'training' => '0', + 'report' => $id, + 'notes' => '', + ], + ]); + $this->{$field_name}->save(); + } + + // Create term and assign rights. + $this->source_random = $this->createTermIfNeeded('source', 9999999, 'Src random', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + } + + /** + * Test adding a report - blocked. + */ + public function testAddReportAsContributorBlockedWithoutRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_unverified->id(), + $this->source_blocked->id(), + $this->source_allowed->id(), + $this->source_trusted->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'refused'); + } + + /** + * Test adding a report - unverified. + */ + public function testAddReportAsContributorUnverifiedWithoutRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_unverified->id(), + $this->source_allowed->id(), + $this->source_trusted->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'on-hold'); + } + + /** + * Test adding a report - allowed. + */ + public function testAddReportAsContributorAllowedWithoutRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_allowed->id(), + $this->source_trusted->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); + } + + /** + * Test adding a report - trusted. + */ + public function testAddReportAsContributorTrustedWithoutRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_trusted->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'published'); + } + + /** + * Test adding a report - blocked. + */ + public function testAddReportAsContributorBlockedWithRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_unverified->id(), + $this->source_blocked->id(), + $this->source_allowed->id(), + $this->source_trusted->id(), + $this->source_random->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'refused'); + } + + /** + * Test adding a report - unverified. + */ + public function testAddReportAsContributorUnverifiedWithRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_unverified->id(), + $this->source_allowed->id(), + $this->source_trusted->id(), + $this->source_random->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(404); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'on-hold'); + } + + /** + * Test adding a report - allowed. + */ + public function testAddReportAsContributorAllowedWithRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_allowed->id(), + $this->source_trusted->id(), + $this->source_random->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // @todo: Fix logic in #RW-1099. + $this->markTestIncomplete( + 'Decide on the proper logic.', + ); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'to-review'); + } + + /** + * Test adding a report - trusted. + */ + public function testAddReportAsContributorTrustedWithRandom() { + $site_name = \Drupal::config('system.site')->get('name'); + $title = $this->randomMachineName(32); + + $this->drupalLogin($this->contributor); + + $edit = $this->getEditFields($title); + $edit['field_source[]'] = [ + $this->source_trusted->id(), + $this->source_random->id(), + ]; + + $this->drupalGet('node/add/report'); + $this->submitForm($edit, 'Submit'); + + // Check that the report has been created. + $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); + $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); + $this->assertSession()->pageTextContains('Belgium'); + $this->assertSession()->pageTextContains('UN Document'); + $this->assertSession()->pageTextContains('English'); + + // @todo: Fix logic in #RW-1099. + $this->markTestIncomplete( + 'Decide on the proper logic.', + ); + + // Check as anonymous. + $this->drupalGet('user/logout'); + $node = $this->getNodeByTitle($title); + $this->drupalGet($node->toUrl()); + $this->assertSession()->statusCodeEquals(200); + + // Check moderation status. + $this->assertEquals($node->moderation_status->value, 'published'); + } + + protected function getEditFields($title) { + $term_language = $this->createTermIfNeeded('language', 267, 'English'); + $term_country = $this->createTermIfNeeded('country', 34, 'Belgium'); + $term_format = $this->createTermIfNeeded('content_format', 11, 'UN Document'); + $term_source = $this->createTermIfNeeded('source', 43679, 'ABC Color', [ + 'field_allowed_content_types' => [ + 1, + ], + ]); + + // Create a node. + $edit = []; + $edit['title[0][value]'] = $title; + $edit['field_language[' . $term_language->id() . ']'] = $term_language->id(); + $edit['field_country[]'] = [$term_country->id()]; + $edit['field_primary_country'] = $term_country->id(); + $edit['field_content_format'] = $term_format->id(); + $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; + $edit['field_source[]'] = [$term_source->id()]; + + return $edit; + } +} From edc031dfa7d45275bbdea9424e6b0b756b5c12e1 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 17 Oct 2024 12:51:30 +0200 Subject: [PATCH 51/79] feat: Logic for status Refs: #RW-1099 --- .../reliefweb_entities/src/Entity/Report.php | 60 +++++++++---------- .../RwReportAddMultipleSourcesTest.php | 20 ++----- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 77927e777..296630b6f 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -392,36 +392,36 @@ protected function updateModerationStatusFromPostingRights() { } // Get the user's posting right for the document. - $right = UserPostingRightsHelper::getUserConsolidatedPostingRight($user, $this->bundle(), $sources); - - // Update the status based on the user's right. - // Note: we don't use `t()` because those are log messages for editors. - switch ($right['name']) { - // Unverified for some sources => on-hold + flag. - case 'unverified': - $status = 'on-hold'; - $message = strtr('Unverified user for @sources.', [ - '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($right['sources'])), - ]); - break; - - // Blocked for some sources => refused + flag. - case 'blocked': - $status = 'refused'; - $message = strtr('Blocked user for @sources.', [ - '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($right['sources'])), - ]); - break; - - // Allowed for all sources => on-hold. - case 'allowed': - $status = 'to-review'; - break; - - // Trusted for all the sources => published. - case 'trusted': - $status = 'published'; - break; + $rights = []; + foreach (UserPostingRightsHelper::getUserPostingRights($user, $sources) as $tid => $data) { + $rights[$data[$this->bundle()] ?? 0][] = $tid; + } + + // Blocked for some sources. + if (!empty($rights[1])) { + $status = 'refused'; + $message = strtr('Blocked user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[1])), + ]); + } + // Trusted for all the sources. + elseif (isset($rights[3]) && count($rights[3]) === count($sources)) { + $status = 'published'; + } + // Trusted for at least 1. + elseif (isset($rights[3]) && count($rights[3]) > 0) { + $status = 'to-review'; + } + // Allowed for at least 1. + elseif (isset($rights[2]) && count($rights[2]) > 0) { + $status = 'to-review'; + } + // Unverified for some sources. + else { + $status = 'on-hold'; + $message = strtr('Unverified user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[0])), + ]); } $this->setModerationStatus($status); diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php index c5c144bb2..a13d6b0c7 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php @@ -170,10 +170,10 @@ public function testAddReportAsContributorUnverifiedWithoutRandom() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); + $this->assertSession()->statusCodeEquals(200); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'on-hold'); + $this->assertEquals($node->moderation_status->value, 'to-review'); } /** @@ -314,10 +314,10 @@ public function testAddReportAsContributorUnverifiedWithRandom() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); + $this->assertSession()->statusCodeEquals(200); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'on-hold'); + $this->assertEquals($node->moderation_status->value, 'to-review'); } /** @@ -346,11 +346,6 @@ public function testAddReportAsContributorAllowedWithRandom() { $this->assertSession()->pageTextContains('UN Document'); $this->assertSession()->pageTextContains('English'); - // @todo: Fix logic in #RW-1099. - $this->markTestIncomplete( - 'Decide on the proper logic.', - ); - // Check as anonymous. $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); @@ -386,11 +381,6 @@ public function testAddReportAsContributorTrustedWithRandom() { $this->assertSession()->pageTextContains('UN Document'); $this->assertSession()->pageTextContains('English'); - // @todo: Fix logic in #RW-1099. - $this->markTestIncomplete( - 'Decide on the proper logic.', - ); - // Check as anonymous. $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); @@ -398,7 +388,7 @@ public function testAddReportAsContributorTrustedWithRandom() { $this->assertSession()->statusCodeEquals(200); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'published'); + $this->assertEquals($node->moderation_status->value, 'to-review'); } protected function getEditFields($title) { From e50ff9fa8fa68e9df311e00045f7839ba11f0172 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 17 Oct 2024 13:34:01 +0200 Subject: [PATCH 52/79] feat: Block saving if user right is blocked Refs: RW-1097 --- .../reliefweb_entities/src/Entity/Source.php | 26 +++++++------ .../src/Services/ReportFormAlter.php | 38 +++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Source.php b/html/modules/custom/reliefweb_entities/src/Entity/Source.php index df559c8e5..e2c008350 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Source.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Source.php @@ -146,18 +146,20 @@ protected function getOrganizationDetails() { $meta = []; // Organization type. - $type = $this->field_organization_type->entity->label(); - $meta['type'] = [ - 'type' => 'link', - 'label' => $this->t('Organization type'), - 'value' => [ - 'url' => RiverServiceBase::getRiverUrl('source', [ - 'search' => 'type.exact:"' . $type . '"', - ]), - 'title' => $type, - 'external' => FALSE, - ], - ]; + if ($this->hasField('field_organization_type') && !$this->get('field_organization_type')->isEmpty()) { + $type = $this->field_organization_type->entity->label(); + $meta['type'] = [ + 'type' => 'link', + 'label' => $this->t('Organization type'), + 'value' => [ + 'url' => RiverServiceBase::getRiverUrl('source', [ + 'search' => 'type.exact:"' . $type . '"', + ]), + 'title' => $type, + 'external' => FALSE, + ], + ]; + } // Headquarters. $countries = []; diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index ee73f401e..5be9c5108 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -9,6 +9,7 @@ use Drupal\reliefweb_entities\Entity\Report; use Drupal\reliefweb_entities\EntityFormAlterServiceBase; use Drupal\reliefweb_form\Helpers\FormHelper; +use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; use Drupal\reliefweb_utility\Helpers\UrlHelper; /** @@ -110,6 +111,43 @@ protected function addBundleFormAlterations(array &$form, FormStateInterface $fo // Prevent saving from a blocked source. $form['#validate'][] = [$this, 'validateBlockedSource']; + + // Prevent saving if user is blocked for a source. + $form['#validate'][] = [$this, 'validatePostingRightsBlockedSource']; + } + + /** + * Prevent saving if user is blocked for a source. + * + * @param array $form + * Form to alter. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + public function validatePostingRightsBlockedSource(array $form, FormStateInterface &$form_state) { + $ids = []; + foreach ($form_state->getValue('field_source', []) as $item) { + if (!empty($item['target_id'])) { + $ids[] = $item['target_id']; + } + } + + $rights = UserPostingRightsHelper::getUserConsolidatedPostingRight($this->currentUser->getAccount(), 'report', $ids); + // Blocked for at least one source. + if (!empty($rights) && isset($rights['code']) && $rights['code'] == 0) { + $sources = $this->getEntityTypeManager() + ->getStorage('taxonomy_term') + ->loadMultiple($rights['sources']); + + /** @var \Drupal\taxonomy\Entity\Term $term */ + array_walk($sources, function (&$term) { + $term = $term->label(); + }); + + $form_state->setErrorByName('field_source', $this->t('Publications from "@sources" are not allowed.', [ + '@sources' => implode('", "', $sources), + ])); + } } /** From c7e4fd57daa9becb003fea068be26f806cf028ee Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 17 Oct 2024 14:16:01 +0200 Subject: [PATCH 53/79] feat: Block saving if user right is blocked Refs: RW-1097 --- .../reliefweb_entities/src/Services/ReportFormAlter.php | 4 ++-- .../src/Helpers/UserPostingRightsHelper.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index 5be9c5108..dcd24f762 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -132,9 +132,9 @@ public function validatePostingRightsBlockedSource(array $form, FormStateInterfa } } - $rights = UserPostingRightsHelper::getUserConsolidatedPostingRight($this->currentUser->getAccount(), 'report', $ids); + $rights = UserPostingRightsHelper::getUserConsolidatedPostingRight($this->currentUser, 'report', $ids); // Blocked for at least one source. - if (!empty($rights) && isset($rights['code']) && $rights['code'] == 0) { + if (!empty($rights) && isset($rights['code']) && $rights['code'] == 1) { $sources = $this->getEntityTypeManager() ->getStorage('taxonomy_term') ->loadMultiple($rights['sources']); diff --git a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php index 99f17ff9f..cb259bd76 100644 --- a/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php +++ b/html/modules/custom/reliefweb_moderation/src/Helpers/UserPostingRightsHelper.php @@ -175,8 +175,8 @@ public static function getUserPostingRights(?AccountInterface $account = NULL, a * the right applies. */ public static function getUserConsolidatedPostingRight(AccountInterface $account, $bundle, array $sources) { - // Not a job nor training or no sources, consider the user 'unverified'. - if (empty($account->uid) || ($bundle !== 'job' && $bundle !== 'training' && $bundle !== 'report') || empty($sources)) { + // Not a job, training nor report or no sources, 'unverified'. + if (empty($account->id()) || ($bundle !== 'job' && $bundle !== 'training' && $bundle !== 'report') || empty($sources)) { return [ 'code' => 0, 'name' => 'unverified', From f03900cb9bec9c4fffd9af0be35ee9ac48e4e8e8 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 17 Oct 2024 14:47:25 +0200 Subject: [PATCH 54/79] test: Block saving if user right is blocked Refs: RW-1097 --- .../RwReportAddMultipleSourcesTest.php | 35 +++--------------- .../src/ExistingSite/RwReportAddTest.php | 36 +++---------------- 2 files changed, 8 insertions(+), 63 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php index a13d6b0c7..2a2409545 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php @@ -123,21 +123,8 @@ public function testAddReportAsContributorBlockedWithoutRandom() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Submit'); - // Check that the report has been created. - $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); - $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); - $this->assertSession()->pageTextContains('Belgium'); - $this->assertSession()->pageTextContains('UN Document'); - $this->assertSession()->pageTextContains('English'); - - // Check as anonymous. - $this->drupalGet('user/logout'); - $node = $this->getNodeByTitle($title); - $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); - - // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'refused'); + // Check that the report has thrown an error. + $this->assertSession()->pageTextContains('Publications from "Src blocked" are not allowed.'); } /** @@ -249,7 +236,6 @@ public function testAddReportAsContributorTrustedWithoutRandom() { * Test adding a report - blocked. */ public function testAddReportAsContributorBlockedWithRandom() { - $site_name = \Drupal::config('system.site')->get('name'); $title = $this->randomMachineName(32); $this->drupalLogin($this->contributor); @@ -266,21 +252,8 @@ public function testAddReportAsContributorBlockedWithRandom() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Submit'); - // Check that the report has been created. - $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); - $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); - $this->assertSession()->pageTextContains('Belgium'); - $this->assertSession()->pageTextContains('UN Document'); - $this->assertSession()->pageTextContains('English'); - - // Check as anonymous. - $this->drupalGet('user/logout'); - $node = $this->getNodeByTitle($title); - $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); - - // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'refused'); + // Check that the report has thrown an error. + $this->assertSession()->pageTextContains('Publications from "Src blocked" are not allowed.'); } /** diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 6ef2434d4..e972ac54c 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -218,22 +218,8 @@ public function testAddReportAsContributorDraftBlocked() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Save as draft'); - // Check that the report has been created. - $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); - $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); - $this->assertSession()->pageTextContains('Belgium'); - $this->assertSession()->pageTextContains('ABC Color'); - $this->assertSession()->pageTextContains('UN Document'); - $this->assertSession()->pageTextContains('English'); - - // Check as anonymous. - $this->drupalGet('user/logout'); - $node = $this->getNodeByTitle($title); - $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); - - // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'draft'); + // Check that the report has thrown an error. + $this->assertSession()->pageTextContains('Publications from "ABC Color" are not allowed.'); } /** @@ -273,22 +259,8 @@ public function testAddReportAsContributorSubmitBlocked() { $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Submit'); - // Check that the report has been created. - $this->assertSession()->titleEquals($title . ' - Belgium | ' . $site_name); - $this->assertSession()->pageTextContains('Report ' . $edit['title[0][value]'] . ' has been created.'); - $this->assertSession()->pageTextContains('Belgium'); - $this->assertSession()->pageTextContains('ABC Color'); - $this->assertSession()->pageTextContains('UN Document'); - $this->assertSession()->pageTextContains('English'); - - // Check as anonymous. - $this->drupalGet('user/logout'); - $node = $this->getNodeByTitle($title); - $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); - - // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'refused'); + // Check that the report has thrown an error. + $this->assertSession()->pageTextContains('Publications from "ABC Color" are not allowed.'); } /** From c0badb0606eda65f978bad3d50b8fb97ae984348 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 14 Nov 2024 13:23:46 +0100 Subject: [PATCH 55/79] chore: Fix wording on dashboard Refs: #RW-1111, #RW-1124 --- .../common_design_subtheme/templates/user/user.html.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/html/themes/custom/common_design_subtheme/templates/user/user.html.twig b/html/themes/custom/common_design_subtheme/templates/user/user.html.twig index a01c893ed..4eb893d1d 100644 --- a/html/themes/custom/common_design_subtheme/templates/user/user.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/user/user.html.twig @@ -41,8 +41,8 @@ {% trans %}Post a job vacancy{% endtrans %}

    {% trans %}Advertise job, consulting and internships vacancies.{% endtrans %}

  • - {% if user.hasPermission('create training content') %} {% endif %} + {% if user.hasPermission('create training content') %}
  • {% trans %}Post a training program{% endtrans %}

    {% trans %}Advertise training programs for the humanitarian community.{% endtrans %}

    @@ -56,7 +56,7 @@ {% endif %}
  • {% trans %}View your posts{% endtrans %} -

    {% trans %}Manage the Job vacancies or Training programs you posted.{% endtrans %}

    +

    {% trans %}Manage the Job vacancies, Training programs or Reports you posted.{% endtrans %}

  • From 9bf79ccebbac34da54c2ac19121489ed647b8f80 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 14 Nov 2024 13:32:38 +0100 Subject: [PATCH 56/79] feat: Add embergoed filter Refs: #RW-1111, #RW-1115 --- .../src/Services/UserPostsService.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index 5ce9ba4a6..dda9c1f15 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -110,14 +110,15 @@ public function getTitle() { */ public function getStatuses() { return [ - 'draft' => $this->t('draft'), - 'pending' => $this->t('pending'), - 'published' => $this->t('published'), - 'on-hold' => $this->t('on-hold'), - 'refused' => $this->t('refused'), - 'expired' => $this->t('expired'), - 'duplicate' => $this->t('duplicate'), - 'to-review' => $this->t('to-review'), + 'draft' => $this->t('Draft'), + 'pending' => $this->t('Pending'), + 'published' => $this->t('Published'), + 'on-hold' => $this->t('On-hold'), + 'refused' => $this->t('Refused'), + 'expired' => $this->t('Expired'), + 'duplicate' => $this->t('Duplicate'), + 'to-review' => $this->t('To review'), + 'embargoed' => $this->t('Embargoed'), ]; } From 4c11c81baa166fdd14c7e5b285b06fce7ee72a28 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 14 Nov 2024 15:51:04 +0100 Subject: [PATCH 57/79] feat: Unverified for single source, to-review Refs: #RW-1111, #RW-1117 --- .../custom/reliefweb_entities/src/Entity/Report.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 296630b6f..4ee2c4301 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -418,7 +418,15 @@ protected function updateModerationStatusFromPostingRights() { } // Unverified for some sources. else { - $status = 'on-hold'; + // If a single source, set status to to-review. + // @see https://humanitarian.atlassian.net/browse/RW-1117 + if (count($sources) == 1) { + $status = 'to-review'; + } + else { + $status = 'on-hold'; + } + $message = strtr('Unverified user for @sources.', [ '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[0])), ]); From 82f8890ce9d30013f0a2a98713ef218bd8b73d22 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 14 Nov 2024 17:00:57 +0100 Subject: [PATCH 58/79] feat: Add reference filter Refs: #RW-1111, #RW-1123 --- .../reliefweb_user_posts/src/Services/UserPostsService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index dda9c1f15..2c771bd8c 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -119,6 +119,7 @@ public function getStatuses() { 'duplicate' => $this->t('Duplicate'), 'to-review' => $this->t('To review'), 'embargoed' => $this->t('Embargoed'), + 'reference' => $this->t('Reference'), ]; } From 25f1dc1527c627b6e8ba25ea039a6602bff4e2b1 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 15 Nov 2024 10:47:50 +0100 Subject: [PATCH 59/79] bug: twig v3.14.1 is broken Refs: #RW-1111, #OPS-11080 --- composer.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/composer.lock b/composer.lock index 292be49d2..4cb2183ce 100644 --- a/composer.lock +++ b/composer.lock @@ -4328,7 +4328,7 @@ "shasum": "fc8ea60619b6b4682bade340e13fb4565d3a7e0c" }, "require": { - "drupal/core": "^9.1 || ^10" + "drupal/core": "^10" }, "type": "drupal-module", "extra": { @@ -4527,7 +4527,7 @@ "shasum": "c25246747dac4372c7d5a5a5fd0f276d9e468eff" }, "require": { - "drupal/core": "^9.3 || ^10", + "drupal/core": "^10", "drupal/mailsystem": "^4" }, "require-dev": { @@ -5432,7 +5432,7 @@ "shasum": "edb3038bf21c327706793cd1328a1734a7db48e6" }, "require": { - "drupal/core": "^10.3 || ^11", + "drupal/core": "^10", "enshrined/svg-sanitize": ">=0.15 <1.0" }, "type": "drupal-module", @@ -5592,7 +5592,7 @@ "shasum": "9ea9eee91cf75f21fcc939704baa6a7ec10d7748" }, "require": { - "drupal/core": "^8.9 || ^9 || ^10" + "drupal/core": "^10" }, "type": "drupal-module", "extra": { @@ -17500,16 +17500,16 @@ }, { "name": "twig/twig", - "version": "v3.14.1", + "version": "v3.14.2", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "f405356d20fb43603bcadc8b09bfb676cb04a379" + "reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/f405356d20fb43603bcadc8b09bfb676cb04a379", - "reference": "f405356d20fb43603bcadc8b09bfb676cb04a379", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a", + "reference": "0b6f9d8370bb3b7f1ce5313ed8feb0fafd6e399a", "shasum": "" }, "require": { @@ -17563,7 +17563,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.14.1" + "source": "https://github.com/twigphp/Twig/tree/v3.14.2" }, "funding": [ { @@ -17575,7 +17575,7 @@ "type": "tidelift" } ], - "time": "2024-11-06T18:17:38+00:00" + "time": "2024-11-07T12:36:22+00:00" }, { "name": "un-ocha/oauth2-hid", From 69f0d96d7b250fa27e31a82ef576e8582a5de55c Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 15 Nov 2024 11:04:23 +0100 Subject: [PATCH 60/79] test: new logic for to-review Refs: #RW-1111 --- .../tests/src/ExistingSite/RwReportAddTest.php | 4 ++-- .../tests/src/ExistingSite/RwReportCreateTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index e972ac54c..15234d489 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -175,10 +175,10 @@ public function testAddReportAsContributorSubmitUnverified() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(404); + $this->assertSession()->statusCodeEquals(200); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'on-hold'); + $this->assertEquals($node->moderation_status->value, 'to-review'); } /** diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index 4cb808165..3b1acb0cd 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -134,9 +134,9 @@ public function testCreateReportAsContributorUnverifiedToReview() { $title = 'My report - unverified'; $this->setUserPostingRightsGetSourceTerm(0, 'Unverified'); $moderation_status = 'to-review'; - $expected_moderation_status = 'on-hold'; + $expected_moderation_status = 'to-review'; - $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, TRUE); } /** From 17e3d3c60a0d83696d1e4b6db370a2e0839e5125 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 15 Nov 2024 11:50:54 +0100 Subject: [PATCH 61/79] feat: Hide fields for contributors Refs: #RW-1111, #RW-1116 --- .../src/Services/ReportFormAlter.php | 33 +++++++ .../form/node-edit-form--report.html.twig | 87 ++++++++++++------- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index dcd24f762..5dabfb662 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -103,6 +103,11 @@ protected function addBundleFormAlterations(array &$form, FormStateInterface $fo ); } + // Special tweaks for contributors. + if ($this->currentUser->hasRole('contributor')) { + $this->alterFieldsForContributors($form, $form_state); + } + // Validate the attachments. $form['#validate'][] = [$this, 'validateAttachment']; @@ -438,4 +443,32 @@ public function validateEmbargoDate(array $form, FormStateInterface &$form_state } } + /** + * Make alterations for Contributor role. + * + * Embargo date cannot be in the past as that would not make sense. + * + * @param array $form + * Form to alter. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * Form state. + */ + protected function alterFieldsForContributors(array &$form, FormStateInterface $form_state) { + // Default to submit. + $form['field_origin']['widget']['#default_value'] = 1; + + // Hide fields. + $form['field_origin']['#access'] = FALSE; + $form['field_origin_notes']['#access'] = FALSE; + + $form['field_bury']['#access'] = FALSE; + $form['field_feature']['#access'] = FALSE; + $form['field_notify']['#access'] = FALSE; + + $form['field_headline']['#access'] = FALSE; + $form['field_headline_title']['#access'] = FALSE; + $form['field_headline_summary']['#access'] = FALSE; + $form['field_headline_image']['#access'] = FALSE; + } + } diff --git a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig index eb7614dbe..ed4b4d63d 100644 --- a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig @@ -18,32 +18,55 @@ {{ attach_library('common_design_subtheme/rw-form') }} {# Table of contents. #} -{{ - render_var({ +{% set field_sections = { + 'content': 'Content'|t, + 'files': 'Files'|t, + 'dates': 'Dates'|t, + 'source': 'Source'|t, + 'countries-disasters': 'Countries & Disasters'|t, + 'details': 'Details'|t, + 'headline': 'Headline'|t, +}%} + +{% set submission_sections = { + 'editorial-flags': 'Editorial flags'|t, + 'actions': 'Save'|t, + 'edit-revision-information': 'Revisions'|t, +}%} + +{% if 'contributor' in user.getroles(TRUE) %} + {% set field_sections = { + 'content': 'Content'|t, + 'files': 'Files'|t, + 'dates': 'Dates'|t, + 'source': 'Source'|t, + 'countries-disasters': 'Countries & Disasters'|t, + 'details': 'Details'|t, + }%} + + {% set submission_sections = { + 'actions': 'Save'|t, + 'edit-revision-information': 'Revisions'|t, + }%} +{% endif %} + +{% set toc = { '#theme': 'reliefweb_entities_table_of_contents', '#sections': { 'fields': { 'title': 'Fields'|t, - 'sections': { - 'content': 'Content'|t, - 'files': 'Files'|t, - 'dates': 'Dates'|t, - 'source': 'Source'|t, - 'countries-disasters': 'Countries & Disasters'|t, - 'details': 'Details'|t, - 'headline': 'Headline'|t, - }, + 'sections': field_sections, }, 'submission': { 'title': 'Submission'|t, - 'sections': { - 'editorial-flags': 'Editorial flags'|t, - 'actions': 'Save'|t, - 'edit-revision-information': 'Revisions'|t, - }, + 'sections': submission_sections, }, }, - }) + } +%} + +{{ + render_var(toc) }} {# Form content. #} @@ -88,20 +111,24 @@ {{ form.field_theme }} -
    - {% trans %}Headline information{% endtrans %} - {{ form.field_headline }} - {{ form.field_headline_title }} - {{ form.field_headline_summary }} - {{ form.field_headline_image }} -
    +{% if not 'contributor' in user.getroles(TRUE) %} +
    + {% trans %}Headline information{% endtrans %} + {{ form.field_headline }} + {{ form.field_headline_title }} + {{ form.field_headline_summary }} + {{ form.field_headline_image }} +
    +{% endif %} -
    - {% trans %}Editorial flags{% endtrans %} - {{ form.field_bury }} - {{ form.field_feature }} - {{ form.field_notify }} -
    +{% if not 'contributor' in user.getroles(TRUE) %} +
    + {% trans %}Editorial flags{% endtrans %} + {{ form.field_bury }} + {{ form.field_feature }} + {{ form.field_notify }} +
    +{% endif %}
    {% trans %}Form submission and other actions{% endtrans %} From 3ea5916a80c2cf68a7cb570a84853a4f1f1de44c Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 15 Nov 2024 12:03:27 +0100 Subject: [PATCH 62/79] test: No origin field needed Refs: #RW-1111, #RW-1116 --- .../tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php | 1 - .../tests/src/ExistingSite/RwReportAddTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php index 2a2409545..c2bf3ea10 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddMultipleSourcesTest.php @@ -381,7 +381,6 @@ protected function getEditFields($title) { $edit['field_country[]'] = [$term_country->id()]; $edit['field_primary_country'] = $term_country->id(); $edit['field_content_format'] = $term_format->id(); - $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; $edit['field_source[]'] = [$term_source->id()]; return $edit; diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 15234d489..5547b2903 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -500,7 +500,6 @@ protected function getEditFields($title) { $edit['field_country[]'] = [$term_country->id()]; $edit['field_primary_country'] = $term_country->id(); $edit['field_content_format'] = $term_format->id(); - $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; $edit['field_source[]'] = [$term_source->id()]; return $edit; From d40d01691a86d4aa97836a16c74a16998a410e9c Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 15 Nov 2024 12:26:08 +0100 Subject: [PATCH 63/79] test: No origin field needed for contributor Refs: #RW-1111, #RW-1116 --- .../tests/src/ExistingSite/RwReportAddTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 5547b2903..56c0d18e4 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -22,6 +22,7 @@ public function testAddReportAsAdminPublished() { $this->drupalLogin($admin); $edit = $this->getEditFields($title); + $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Publish'); @@ -52,6 +53,7 @@ public function testAddReportAsAdminDraft() { $this->drupalLogin($admin); $edit = $this->getEditFields($title); + $edit['field_origin_notes[0][value]'] = 'https://example.com/' . $title; $this->drupalGet('node/add/report'); $this->submitForm($edit, 'Save as draft'); From f85e704be1917952d283047f9b11640993707cae Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Wed, 20 Nov 2024 09:25:11 +0100 Subject: [PATCH 64/79] Update html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php Co-authored-by: orakili --- .../custom/reliefweb_entities/src/Services/ReportFormAlter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index 5dabfb662..161b9accc 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -446,8 +446,6 @@ public function validateEmbargoDate(array $form, FormStateInterface &$form_state /** * Make alterations for Contributor role. * - * Embargo date cannot be in the past as that would not make sense. - * * @param array $form * Form to alter. * @param \Drupal\Core\Form\FormStateInterface $form_state From 6ab6a7af2b83592a8ed142d3bd91aa719263c09a Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Wed, 20 Nov 2024 09:25:26 +0100 Subject: [PATCH 65/79] Update html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css Co-authored-by: orakili --- .../common_design_subtheme/components/rw-people/rw-people.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css b/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css index c583023b6..cad2fa7ed 100644 --- a/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css +++ b/html/themes/custom/common_design_subtheme/components/rw-people/rw-people.css @@ -120,7 +120,7 @@ height: 12px; border-radius: 4px; background: var(--cd-reliefweb-orange); - color:rgba(0,0,0,0); + color: rgba(0,0,0,0); } .posting-rights--wrapper .posting-rights[data-posting-right="1"] { background: var(--cd-reliefweb-red); From c5c315ccc3e25d9a51a8f651f6d125220faea9d9 Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Wed, 20 Nov 2024 09:25:34 +0100 Subject: [PATCH 66/79] Update html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig Co-authored-by: orakili --- .../templates/form/node-edit-form--report.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig index ed4b4d63d..49f1a7f02 100644 --- a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig @@ -111,7 +111,7 @@ {{ form.field_theme }}
    -{% if not 'contributor' in user.getroles(TRUE) %} +{% if 'contributor' not in user.getroles(TRUE) %}
    {% trans %}Headline information{% endtrans %} {{ form.field_headline }} From 53e81c671efd1b6cf85b657d3f41213600b8e3e8 Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Thu, 21 Nov 2024 13:52:44 +0100 Subject: [PATCH 67/79] Update html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig Co-authored-by: orakili --- .../templates/form/node-edit-form--report.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig index 49f1a7f02..5d96b44f5 100644 --- a/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/form/node-edit-form--report.html.twig @@ -121,7 +121,7 @@
    {% endif %} -{% if not 'contributor' in user.getroles(TRUE) %} +{% if 'contributor' not in user.getroles(TRUE) %}
    {% trans %}Editorial flags{% endtrans %} {{ form.field_bury }} From 5e5fca9892710baff4e10545651228b2e680a02d Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 13:55:19 +0100 Subject: [PATCH 68/79] feat: Add a if for reports and use N/A or something like that as value so there is no missing cell for reports in the My posts table. --- .../reliefweb_user_posts/src/Services/UserPostsService.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index 2c771bd8c..e04cfbfb8 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -236,6 +236,9 @@ public function getRows(array $results) { elseif ($entity->bundle() === 'job') { $cells['deadline'] = $this->formatDate($entity->field_job_closing_date->value); } + else { + $cells['deadline'] = $this->t('N/A'); + } $rows[] = $cells; } From 77ff6f5f24ab0742ae9212eda46cb102a1b6d395 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 14:01:24 +0100 Subject: [PATCH 69/79] feat: I do not think this is useful for reports and is not currently not displayed anyway. --- .../reliefweb_entities/src/EntityFormAlterServiceBase.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php b/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php index 7c8381f2e..985b28fed 100644 --- a/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php +++ b/html/modules/custom/reliefweb_entities/src/EntityFormAlterServiceBase.php @@ -384,8 +384,8 @@ protected function addPotentialNewSourceFields(array &$form, FormStateInterface $entity = $form_state->getFormObject()->getEntity(); $bundle = $entity->bundle(); - // The following only applies to job, training and report nodes. - if (!in_array($bundle, ['job', 'training', 'report'])) { + // The following only applies to job and training nodes. + if (!in_array($bundle, ['job', 'training'])) { return; } @@ -594,8 +594,8 @@ protected function addUserInformation(array &$form, FormStateInterface $form_sta $entity_id = $entity->id(); $bundle = $entity->bundle(); - // It's only for jobs, trainings and reports. - if (!in_array($bundle, ['job', 'training', 'report'])) { + // It's only for jobs and trainings. + if (!in_array($bundle, ['job', 'training'])) { return; } From 250b40f028a98e44f49914611cad50168e835d9f Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 14:08:24 +0100 Subject: [PATCH 70/79] feat: Body format "markdown" should not be allowed for contributor --- config/filter.format.markdown.yml | 2 +- config/user.role.authenticated.yml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/config/filter.format.markdown.yml b/config/filter.format.markdown.yml index ae94b2e9e..63c51a4c0 100644 --- a/config/filter.format.markdown.yml +++ b/config/filter.format.markdown.yml @@ -82,4 +82,4 @@ filters: status: false weight: 0 settings: - replace_empty: '0' + replace_empty: 0 diff --git a/config/user.role.authenticated.yml b/config/user.role.authenticated.yml index 4d6f02efa..5b47d3617 100644 --- a/config/user.role.authenticated.yml +++ b/config/user.role.authenticated.yml @@ -3,7 +3,6 @@ langcode: en status: true dependencies: config: - - filter.format.markdown - filter.format.markdown_editor - filter.format.token_markdown - node.type.job @@ -49,7 +48,6 @@ permissions: - 'subscribe to jobs' - 'subscribe to training' - 'use enhanced input forms' - - 'use text format markdown' - 'use text format markdown_editor' - 'use text format token_markdown' - 'view media' From 3f03cd344f9e87a9997b1982aa478c8aacafea7f Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 14:21:47 +0100 Subject: [PATCH 71/79] feat: Source guidelines should only be shown to editors --- .../custom/reliefweb_form/src/Controller/NodeForm.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_form/src/Controller/NodeForm.php b/html/modules/custom/reliefweb_form/src/Controller/NodeForm.php index 59c210194..c91a898ba 100644 --- a/html/modules/custom/reliefweb_form/src/Controller/NodeForm.php +++ b/html/modules/custom/reliefweb_form/src/Controller/NodeForm.php @@ -51,7 +51,10 @@ public function getSourceAttentionMessages($bundle) { // For reports, we simply load the messages from the report attention field. if ($bundle === 'report') { - $messages = $this->loadSourceAttentionMessages($bundle, $ids); + // No messages for contributors. + if (!$this->currentUser()->hasRole('contributor')) { + $messages = $this->loadSourceAttentionMessages($bundle, $ids); + } } // For jobs or training, we combine the job and training attention messages // as the information is useful for both teams. From 3d25a2453131483a51d06319ab9b0c60e04a7316 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 14:38:17 +0100 Subject: [PATCH 72/79] feat: Statuses irrelevant to job/training displayed on My Posts page of normal users without any roles --- .../src/Services/UserPostsService.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php index e04cfbfb8..b77f3ea9f 100644 --- a/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php +++ b/html/modules/custom/reliefweb_user_posts/src/Services/UserPostsService.php @@ -109,7 +109,7 @@ public function getTitle() { * {@inheritdoc} */ public function getStatuses() { - return [ + $statuses = [ 'draft' => $this->t('Draft'), 'pending' => $this->t('Pending'), 'published' => $this->t('Published'), @@ -117,10 +117,17 @@ public function getStatuses() { 'refused' => $this->t('Refused'), 'expired' => $this->t('Expired'), 'duplicate' => $this->t('Duplicate'), - 'to-review' => $this->t('To review'), - 'embargoed' => $this->t('Embargoed'), - 'reference' => $this->t('Reference'), ]; + + if ($this->currentUser->hasRole('contributor')) { + $statuses += [ + 'to-review' => $this->t('To review'), + 'embargoed' => $this->t('Embargoed'), + 'reference' => $this->t('Reference'), + ]; + } + + return $statuses; } /** From 766ff2e4b75e6f131529ea9833082923fd7e8197 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 21 Nov 2024 14:53:50 +0100 Subject: [PATCH 73/79] =?UTF-8?q?feat:=20Reports=20posted=20by=20editors?= =?UTF-8?q?=20appear=20as=20"Unverified"=20=E2=86=92=20probably=20should?= =?UTF-8?q?=20not=20be=20displayed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reliefweb_moderation/src/Services/ReportModeration.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index 9fbe7d04a..f14462a30 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -6,6 +6,7 @@ use Drupal\Core\Link; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\node\NodeInterface; use Drupal\reliefweb_moderation\EntityModeratedInterface; use Drupal\reliefweb_moderation\Helpers\UserPostingRightsHelper; use Drupal\reliefweb_moderation\ModerationServiceBase; @@ -110,8 +111,12 @@ public function getRows(array $results) { // Country and source info. $info = []; + // User posting rights. - $info['posting_rights'] = UserPostingRightsHelper::renderRight(UserPostingRightsHelper::getEntityAuthorPostingRights($entity)); + if ($entity instanceof NodeInterface && $entity->getOwner()->hasRole('contributor')) { + $info['posting_rights'] = UserPostingRightsHelper::renderRight(UserPostingRightsHelper::getEntityAuthorPostingRights($entity)); + } + // Country. $country_link = $this->getTaxonomyTermLink($entity->field_primary_country->first()); if (!empty($country_link)) { From 1626c30531d11d678f6b56609f1c852878559049 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 26 Nov 2024 11:11:24 +0100 Subject: [PATCH 74/79] feat: URL optional for contributors Refs: #RW-1111 --- .../reliefweb_entities/src/Services/ReportFormAlter.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index 161b9accc..9c6fb964f 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -66,8 +66,10 @@ protected function addBundleFormAlterations(array &$form, FormStateInterface $fo $this->alterHeadlineFields($form, $form_state); // Alter the origin fields, setting the origin notes as mandatory when - // 'URL' is selected. - $this->alterOriginFields($form, $form_state); + // 'URL' is selected, except for contributors. + if (!$this->currentUser->hasRole('contributor')) { + $this->alterOriginFields($form, $form_state); + } // Alter the OCHA product field, ensuring only 1 is selectable and making // mandatory when OCHA is selected as source or hidden otherwise. @@ -456,9 +458,6 @@ protected function alterFieldsForContributors(array &$form, FormStateInterface $ $form['field_origin']['widget']['#default_value'] = 1; // Hide fields. - $form['field_origin']['#access'] = FALSE; - $form['field_origin_notes']['#access'] = FALSE; - $form['field_bury']['#access'] = FALSE; $form['field_feature']['#access'] = FALSE; $form['field_notify']['#access'] = FALSE; From c3a9bf87310580e593e6336ce2698c338b87f725 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Tue, 26 Nov 2024 14:22:19 +0100 Subject: [PATCH 75/79] feat: Contributir can use on-hold Refs: #RW-1111 --- .../reliefweb_entities/src/Entity/Report.php | 2 +- .../src/Services/ReportModeration.php | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 4ee2c4301..0cb461a30 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -380,7 +380,7 @@ protected function updateModerationStatusFromPostingRights() { // For non editors, we determine the real status based on the user // posting rights for the selected sources. - if (!UserHelper::userHasRoles(['editor'], $user) && in_array($status, ['on-hold', 'to-review'])) { + if (!UserHelper::userHasRoles(['editor'], $user) && in_array($status, ['to-review'])) { // Retrieve the list of sources and check the user rights. if (!$this->field_source->isEmpty()) { // Extract source ids. diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index f14462a30..db523c752 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -283,14 +283,18 @@ public function getEntityFormSubmitButtons($status, EntityModeratedInterface $en ], ]; } - // Other users can submit for review (or publish directly if trusted). + // Other users can submit for review, on-hold or published if trusted. else { - $buttons['draft'] = [ - '#value' => $this->t('Save as draft'), - ]; - - $buttons['to-review'] = [ - '#value' => $new ? $this->t('Submit') : $this->t('Submit changes'), + $buttons = [ + 'draft' => [ + '#value' => $this->t('Save as draft'), + ], + 'to-review' => [ + '#value' => $new ? $this->t('Submit') : $this->t('Submit changes'), + ], + 'on-hold' => [ + '#value' => $this->t('On-hold'), + ], ]; // Add confirmation when attempting to change published document. From 6b5ce4fd243ed0ec88c0de553b2fd8e01d7fc19a Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 28 Nov 2024 14:39:55 +0100 Subject: [PATCH 76/79] feat: Posting rights --- .../custom/reliefweb_entities/src/Entity/Report.php | 12 ++++-------- .../tests/src/ExistingSite/RwReportAddTest.php | 4 ++-- .../tests/src/ExistingSite/RwReportCreateTest.php | 8 ++++---- .../src/Services/ReportModeration.php | 1 + 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index 0cb461a30..a19b0c181 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -411,6 +411,9 @@ protected function updateModerationStatusFromPostingRights() { // Trusted for at least 1. elseif (isset($rights[3]) && count($rights[3]) > 0) { $status = 'to-review'; + $message = strtr('Allowed user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[3])), + ]); } // Allowed for at least 1. elseif (isset($rights[2]) && count($rights[2]) > 0) { @@ -418,14 +421,7 @@ protected function updateModerationStatusFromPostingRights() { } // Unverified for some sources. else { - // If a single source, set status to to-review. - // @see https://humanitarian.atlassian.net/browse/RW-1117 - if (count($sources) == 1) { - $status = 'to-review'; - } - else { - $status = 'on-hold'; - } + $status = 'pending'; $message = strtr('Unverified user for @sources.', [ '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[0])), diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php index 56c0d18e4..d393c0f69 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportAddTest.php @@ -177,10 +177,10 @@ public function testAddReportAsContributorSubmitUnverified() { $this->drupalGet('user/logout'); $node = $this->getNodeByTitle($title); $this->drupalGet($node->toUrl()); - $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->statusCodeEquals(404); // Check moderation status. - $this->assertEquals($node->moderation_status->value, 'to-review'); + $this->assertEquals($node->moderation_status->value, 'pending'); } /** diff --git a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php index 3b1acb0cd..a80081322 100644 --- a/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php +++ b/html/modules/custom/reliefweb_entities/tests/src/ExistingSite/RwReportCreateTest.php @@ -128,15 +128,15 @@ public function testCreateReportAsContributorUnverifiedDraft() { } /** - * Test report as contributor unverified, to-review. + * Test report as contributor unverified, pending. */ public function testCreateReportAsContributorUnverifiedToReview() { $title = 'My report - unverified'; $this->setUserPostingRightsGetSourceTerm(0, 'Unverified'); - $moderation_status = 'to-review'; - $expected_moderation_status = 'to-review'; + $moderation_status = 'pending'; + $expected_moderation_status = 'pending'; - $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, TRUE); + $this->runTestCreateReportAsContributor($title, $moderation_status, $expected_moderation_status, FALSE); } /** diff --git a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php index db523c752..fa7d19e2e 100644 --- a/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php +++ b/html/modules/custom/reliefweb_moderation/src/Services/ReportModeration.php @@ -209,6 +209,7 @@ public function getStatuses() { 'draft' => $this->t('Draft'), 'on-hold' => $this->t('On-hold'), 'to-review' => $this->t('To review'), + 'pending' => $this->t('Pending'), 'published' => $this->t('Published'), 'embargoed' => $this->t('Embargoed'), 'refused' => $this->t('Refused'), From d1b69dd229f79f36d40791d0c65abe8e5b34eb34 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 28 Nov 2024 14:56:34 +0100 Subject: [PATCH 77/79] chore: docksal --- .docksal/docksal-local.env | 2 ++ .docksal/docksal.env | 2 +- .docksal/etc/mysql/my.cnf | 2 ++ .docksal/etc/php/php.ini | 3 +-- 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 .docksal/etc/mysql/my.cnf diff --git a/.docksal/docksal-local.env b/.docksal/docksal-local.env index 8ecd27dba..c49944789 100644 --- a/.docksal/docksal-local.env +++ b/.docksal/docksal-local.env @@ -1 +1,3 @@ MYSQL_PORT_MAPPING='3380:3306' +XDEBUG_ENABLED=1 +XDEBUG_MODE=coverage diff --git a/.docksal/docksal.env b/.docksal/docksal.env index 7da5888bd..8923009e6 100644 --- a/.docksal/docksal.env +++ b/.docksal/docksal.env @@ -1,4 +1,4 @@ DOCKSAL_STACK=default DOCROOT=html DB_IMAGE="docksal/mariadb:10.6" -CLI_IMAGE='docksal/cli:php8.2-build' +CLI_IMAGE='docksal/cli:php8.3-build' diff --git a/.docksal/etc/mysql/my.cnf b/.docksal/etc/mysql/my.cnf new file mode 100644 index 000000000..5a9ce8345 --- /dev/null +++ b/.docksal/etc/mysql/my.cnf @@ -0,0 +1,2 @@ +[mysqld] +innodb_force_recovery=0 diff --git a/.docksal/etc/php/php.ini b/.docksal/etc/php/php.ini index 5e25967e8..cea362e77 100644 --- a/.docksal/etc/php/php.ini +++ b/.docksal/etc/php/php.ini @@ -1,2 +1 @@ -; Mail settings -sendmail_path = '/usr/bin/msmtp -t --host=mail --port=1025 --read-envelope-from' +xdebug.mode=coverage From 85cc1b34d1c5fcf620798e2bc0c24fe65630afc7 Mon Sep 17 00:00:00 2001 From: orakili Date: Wed, 4 Dec 2024 05:10:59 +0000 Subject: [PATCH 78/79] fix: status when unverified and allowed and add messages with the different user rights for the sources Refs: RW-1058 --- .../reliefweb_entities/src/Entity/Report.php | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Entity/Report.php b/html/modules/custom/reliefweb_entities/src/Entity/Report.php index a19b0c181..d4dbc56be 100644 --- a/html/modules/custom/reliefweb_entities/src/Entity/Report.php +++ b/html/modules/custom/reliefweb_entities/src/Entity/Report.php @@ -400,9 +400,6 @@ protected function updateModerationStatusFromPostingRights() { // Blocked for some sources. if (!empty($rights[1])) { $status = 'refused'; - $message = strtr('Blocked user for @sources.', [ - '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[1])), - ]); } // Trusted for all the sources. elseif (isset($rights[3]) && count($rights[3]) === count($sources)) { @@ -411,25 +408,41 @@ protected function updateModerationStatusFromPostingRights() { // Trusted for at least 1. elseif (isset($rights[3]) && count($rights[3]) > 0) { $status = 'to-review'; - $message = strtr('Allowed user for @sources.', [ - '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[3])), - ]); } - // Allowed for at least 1. - elseif (isset($rights[2]) && count($rights[2]) > 0) { + // Allowed for all the sources. + elseif (isset($rights[2]) && count($rights[2]) === count($sources)) { $status = 'to-review'; } // Unverified for some sources. else { $status = 'pending'; - - $message = strtr('Unverified user for @sources.', [ - '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[0])), - ]); } $this->setModerationStatus($status); + // Add messages indicating the posting rights for easier review. + $message = ''; + if (!empty($rights[1])) { + $message = trim($message . strtr(' Blocked user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[1])), + ])); + } + if (!empty($rights[0])) { + $message = trim($message . strtr(' Unverified user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[0])), + ])); + } + if (!empty($rights[2])) { + $message = trim($message . strtr(' Allowed user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[2])), + ])); + } + if (!empty($rights[3])) { + $message = trim($message . strtr(' Trusted user for @sources.', [ + '@sources' => implode(', ', TaxonomyHelper::getSourceShortnames($rights[3])), + ])); + } + // Update the log message. if (!empty($message)) { $revision_log_field = $this->getEntityType() From 685a96179d53a38c2b6fe0a3b140bd091048ec54 Mon Sep 17 00:00:00 2001 From: orakili Date: Wed, 4 Dec 2024 05:12:05 +0000 Subject: [PATCH 79/79] fix: hide origin field but perserve validation of origin notes Refs: RW-1058 --- .../src/Services/ReportFormAlter.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php index 9c6fb964f..d52c58e15 100644 --- a/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php +++ b/html/modules/custom/reliefweb_entities/src/Services/ReportFormAlter.php @@ -66,10 +66,8 @@ protected function addBundleFormAlterations(array &$form, FormStateInterface $fo $this->alterHeadlineFields($form, $form_state); // Alter the origin fields, setting the origin notes as mandatory when - // 'URL' is selected, except for contributors. - if (!$this->currentUser->hasRole('contributor')) { - $this->alterOriginFields($form, $form_state); - } + // 'URL' is selected. + $this->alterOriginFields($form, $form_state); // Alter the OCHA product field, ensuring only 1 is selectable and making // mandatory when OCHA is selected as source or hidden otherwise. @@ -454,8 +452,15 @@ public function validateEmbargoDate(array $form, FormStateInterface &$form_state * Form state. */ protected function alterFieldsForContributors(array &$form, FormStateInterface $form_state) { - // Default to submit. - $form['field_origin']['widget']['#default_value'] = 1; + // Default to submit for new documents otherwise preserve the value, for + // example when editing a report created by an editor. + if ($form_state->getFormObject()?->getEntity()?->isNew() === TRUE) { + $form['field_origin']['widget']['#default_value'] = '1'; + } + // Change the field to 'hidden' to hide it while perserving its value so + // that the alteration and validation of the origin notes field still work. + // @see ::alterOriginFields() + $form['field_origin']['widget']['#type'] = 'hidden'; // Hide fields. $form['field_bury']['#access'] = FALSE;