0

I wrote an update hook for a site that moves data from one field to another. It looks a lot like this gist:

  foreach ($node_ids as $node_id) {
    try {
      $node_wrapper = entity_metadata_wrapper('node', $node_id);
      $node_wrapper->field_new_field->set($node_wrapper->field_old_field[0]->value());
      $node_wrapper->save();
    }
    catch (Exception $e) {
      watchdog('mymodule', 'Problem with node @node_id', array('@node_id' => $node_id), WATCHDOG_ERROR);
    }
    $sandbox['progress']++;
  }

It's being tested on CI (Travis) which runs "drush updatedb -y" so what I was wondering is if there is a way to query a particular node and make sure the field is populated? Like for example make sure the data in field_new_field is the same as _field_old_field and printing that on the command line?

3

Yes, very quickly. You already have part of the code. Since you have already loaded the node via the entity_metadata_wrapper() you have all of the information at your finger tips.

An example is this in your hook_update_N()

foreach ($node_ids as $node_id) {
    try {
      $node_wrapper = entity_metadata_wrapper('node', $node_id);
      $node_wrapper->field_new_field->set($node_wrapper->field_old_field[0]->value());
      $node_wrapper->save();
      if ($node_wrapper->field_new_field->value() == $node_wrapper->field_old_field->value()){
        print_r('Success. Old field data is in new field data');
      }
    }
    catch (Exception $e) {
      watchdog('mymodule', 'Problem with node @node_id', array('@node_id' => $node_id), WATCHDOG_ERROR);
    }
    $sandbox['progress']++;
  }

I added a comparison of the old field data and the new field data. If it is the same, it will print 'Success. Old field data is in new field data' in the console of Travis-CI. You can do this for any entity. You can use print_r() to output data that you can see in Travis-CI's console. I do this for troubleshooting my test scripts when I may be using a new test method I have never used before and I want to see what Drupal is giving me. Most of the modules I maintain on D.O are tested with Travis-CI.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.