MEXICO CITY — The new president of Mexico, Andrés Manuel López Obrador, has built his entire political career on defending the poor.

Now, days before Mr. López Obrador takes office, President Trump is testing how firmly he will live up to that.

Thousands of migrants from Central America have massed along the border of Mexico and the United States — with thousands more on the way. American border patrol agents fired tear gas at them on Sunday to prevent hundreds from reaching the border.

Mr. Trump has vowed to keep the migrants on Mexican soil while they apply for asylum in the United States, a process that could squeeze them into squalid, overcrowded shelters for months, possibly even years. Mexican officials say the strain is already causing a humanitarian emergency, creating a political crisis for Mr. López Obrador even before he takes office.